In many cases WordPress developers want to display specific content only on homepage. In other cases they’ll want to display some content only on subpages and not on the homepage. This content can be any kind of HTML elements (text, images…), event template parts such as widget areas (sidebars) or headers and footers (different on homepage and subpages). For that purpouse we’ll use is_front_page and is_home functions.
Difference between is_front_page and is_home functions
WordPress by default has two different homepage layouts. One layout displays a list of latest blog posts and the other shows a static page. To choose between this two layouts click on Settings in the Dashboard left column menu and then choose the Reading section. Homepage layout is defined in the first option of the Reading Settings screen.
The difference between is_front_page and is_home functions is that each function is used for one of the homepage layouts:
- is_home function is used when homepage displays the latest blog posts
- is_front_page function is used when homepage displays a static page
Adding is_home() and is_front_page() functions to WordPress theme files
WordPress functions is_front_page and is_home functions work properly only when added to the template files that generate the home page. Usually the home page is genrated form header.php , footer.php and the file that generates the content section. That file varies from theme to theme and sometimes it requires some intermediate understanding of WordPress file structure to locate it.
For example, in the Twenty Nineteen WordPress theme, the file that generates the content part of the a static page is located in the theme subfolder /theme-parts/content/ and its called content-page.php.
PRO TIP:Pro Tip: Before you start editing your WordPress theme, consider creating a Child theme if you’re not already using one. Editing a Child theme will keep your modifications safe when updating the main theme. We also suggest using a Code highlighter plugin for easier editing of WordPress theme files.
How to show or hide content on the home page and subpages in WordPress
In our example we’ll display some custom content between the page header and content. Firstly we have to locate the header template file inside the WordPress theme editor. Secondly we have to position the cursor inside the template file where we want to add our custom content. In most cases that position will be right after the closing </header> tag.
If the homepage is set up to display the latest blog posts, insert the following code:
<?php if ( is_home() ) : echo '<p id=custom-content>'; echo 'Welcome to our Homepage!' ; echo '</p>'; else : echo '<p id=custom-content>'; echo 'This is one of our site subpages.' ; echo '</p>'; endif; ?>
If the homepage is set up to display a static page, insert the following code:
<?php if ( is_front_page() ) : echo '<p id=custom-content>'; echo 'Welcome to our Homepage!' ; echo '</p>'; else : echo '<p id=custom-content>'; echo 'This is one of our site subpages.' ; echo '</p>'; endif; ?>
By slight modifications of the code snippets above you can show or hide any HTML element or WordPress template part on your home page or on subpages.