WordPress Conditional Home or Front Page Function

October 23, 2015   /   by Peter Johnson  / Categories :  Theme Coding

WordPress uses different templates for pages on your site. If you have a page.php template in your theme, that will be used to display your pages. If you have a single.php, that will be used to display your single posts. index.php or home.php would display your home page, or page.php if you have a certain page selected as the home page through the reading options in the Admin panel.

To select options we can use a conditional statement to make choices on what to show on the home page or a selected front page.

This would need to be added to the template page being used.

<
?php if (is_front_page()){ ?>

Home Page

<
?php else { ?>

Not Home Page

<
?php } ?>

It is good practice to add both WordPress functions to a theme depending on if a page has been selected for a front page.

<
?php if (is_front_page() || is_home()){ ?>

Home Page

<
?php else { ?>

Not Home Page

<
?php } ?>

Working the oposite way around.

<
?php if (!is_front_page() && !is_home()){ ?>

Not Home Page

<
?php else { ?>

Home Page

<
?php } ?>

0 comments

Leave a reply