Using Conditional Tags For Home, Front & Blog Pages

In this post, i’ll teach you what the difference is between the 4 conditional tags for:

  • Your Blog page
  • Your Home Page
  • Your Front Page
  • Your Blog Page Template

The code you should use depends a lot on your Reading Settings.

Blog Page

If you want to display content only on your blog page, the same conditional tag you use may not work in all circumstances.

It depends on your Reading settings. If you’re set your Reading settings to display a static page on the front page and selected your blog page as the page display post on, then using the is_home() conditional tag in a custom function in your child theme, will display your content on your blog page.

reading settings

Example Code:

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if(is_home() ) {
echo "Hello World";
}}

This code will not work if you have not selected the exact same settings in the above screenshot.

It doesn’t matter how many pages you have used with the blog page template. Only the one selected in the reading settings for your Posts Page will display the content you use in a custom function with the is_home() conditional tag.

Your static front page will not display the content when using the is_home() conditional tag.

Another method is to use the genesis_is_blog_template() conditional tag to target any page using the Genesis blog page template.

Home Page

Here’s the Reading Settings which work with the is_home() conditional tag.

latest posts

If you’re using your front page to display your posts which many themes do by default like shown in the screenshot above, then you can use the is_home() conditional tag in a custom function like this:

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if(is_home() ) {
echo "Hello World";
}}

The above code will display your content on the front page as long as you haven’t changed the default Reading Settings.

It doesn’t matter if you have setup a custom blog page as well and used the blog page template your themes provides an an option under your Page Attributes. In this case the is_home() conditional tag will not display your content on your blog page.

You can also use the is_front_page() conditional tag in this case or even use them together.

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if(is_home() AND is_front_page() ) {
echo "Hello World";
}}

Or like this:

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if(is_home() || is_front_page() ) {
echo "Hello World";
}}

Front Page

If you have selected to use a static page as your front page, you can use the conditional tag is_front_page to display content on your front page.

static page

Here’s the code you can use in your child themes functions.php file.

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if(is_front_page() ) {
echo "Hello World";
}}

You can also use this conditional tag even if you have selected to display your latest posts which is the default setting.

front page

Blog Page Template

Genesis users can also use the is_page_template() conditional tag to display content only on pages using this template.

If you want to use this conditional tag to display content on your blog page, you will also need to use the correct Reading Settings.

Firstly, here’s a working example of how to use the is_page_template() to display content on your blog page.

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if ( genesis_is_blog_template() ) 
echo "Hello World";
}

Using page_blog-php or blog-php will not work so you need to be extra careful to make sure you get it right.

Using the code snippet above only works when you use the correct Reading Settings.

The Reading Settings selected in the below screenshot won’t display your content on the page you have selected as your Posts Page when using this code above.

The code will work to display content on any other page using the blog page template which probably isn’t want you want.

blog page template

If you use another page as your posts page and also have created a different page for your blog like in this screenshot below, then the code will work to display your content on all other pages using the blog page template and exclude the page you selected as your Posts Page.

page for posts

The default settings which you see in the screenshot below will make the code display your content on any pages using the blog page template but that’s also probably not what you want because the default settings also make your latest posts display on the home page so you won’t need a custom blog page.

default reading settings

So What Works?

As shown earlier, the is_home() conditional works for displaying content on your blog page when you have selected a static page as your front page and your blog page as your posts page in your Reading Settings.

add_action('genesis_before_loop', 'content_before_blog_page');
function content_before_blog_page() {
if (is_home() ) {
echo "Hello World";
}}

You don’t even need to select the blog page template or even name your page blog or even use a permalink with the name blog. It works based on what you select in the Reading Settings.

Learn More About Adding a Widget Before Loop On First Page Of Posts Page


Comments

11 responses to “Using Conditional Tags For Home, Front & Blog Pages”

  1. […] Using Conditional Tags For Home, Front & Blog Pages A nice run-down on the variety of potential scenarios that can manifest given various selections made from template tag to CMS selections.  Home Page and Front Page and Templates, Oh My! Websites , WordPress ← Web Design Trends: Spring 2018 Home | About […]

  2. I was just searching for is_blog() conditional- its the first that popped up in my head when I cam across this problem

    is_blog() makes total sense – so it will probably never happen 🙂

    1. Brad Dalton Avatar
      Brad Dalton

      Yeah its an idea but i guess there must be a reason why it isn’t used already.

      is_home() is a bit confusing for me anyway until i got my head around it.

      Thanks for the comment Tye.

  3. Nice article, Brad. As usual! Just one quick question… there are any differences between using the is_page_template() on functions.php VS using the template file of that template? Or it is just a developer’s choice?

    1. Brad Dalton Avatar
      Brad Dalton

      Could you clarify your question please Joan.

      1. Yeah, sure!

        I mean if there is any difference between a conditional like is_page_template(page_special-php) in functions.php instead putting the code in page_special.php.

        1. Brad Dalton Avatar
          Brad Dalton

          If you put the code in the template then you don’t need the conditional tag in your functions file.

          The problem is there’s no templates for archives including the blog page in the child themes files which is why you need to using conditional tags in your child themes functions file.

          1. Ok, now I get it! Thanks 🙂

  4. Brad,

    I was just thinking about this very issue but with a twist. Using the Genesis Minimum-Pro theme I want the site tagline to differ on each page. So I’m thinking to create conditional tags that test for page ID and call a registered widget. A widget would make sense so the text could be changed easily in the future. So, if the page were a “services page” where ti ID was something like this…post=1136 could you use a series of filters testing the page ID and then calling a registered widget?

    1. Brad Dalton Avatar
      Brad Dalton

      Hi John

      I have nearly finished coding the Minimum Pro tagline with a custom field so you can simply add any type of content in a new meta box on any edit screen.

      Finished the PHP but need to work out how to style the text as i didn’t add a div class.

      1. I’ll keep my eyes peeled for your solution.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.