Using WordPress Homepage Conditionals is_front_page & is_home The Right Way

Using WordPress Homepage Conditionals is_front_page & is_home The Right Way

WordPress is a terrific CMS. You can do whatever you want with it. But sometimes, you may find an issue with some basic functions. For example, you are probably using the is_front_page and is_home WordPress Homepage┬áconditional in your themes and plugins (you can learn more about conditional functions in our Conditional Tags post). But you’re doing it wrong !

Homepage Settings

With those conditional functions, you can check if the homepage is being displayed (is_home), or if the homepage is using a static page (is_front_page). You can define such behaviour in Settings=> Reading.

Reading settings

Reading settings

In your plugin or theme, you may use this functions to display or change contents depending on user settings. But you are doing a huge mistake : you are not taking into account the static blog page. When you define a static page for your homepage, you can also define a blog page : it will replace the default WordPress homepage in order to list every post.

Long story made short : is_front_page should always be tested with and before is_home. And here’s why.

How It Works

1. Normal settings:

The default WordPress homepage lists your latest posts.

  • Here is the URL : website.com
  • is_home returns TRUE
  • is_front_page returns TRUE

Here, there isn’t any issue.

2. Static homepage:

The homepage is using one of your static pages.

  • The URL is still website.com
  • is_home returns FALSE
  • is_front_page returns TRUE

There is no issue with your static homepage : is_front_page returns true.

3. Static blog page

One of your page is listing all your post.

  • The URL is website.com/pagename
  • is_home : TRUE
  • is_front_page : FALSE

Here, you may have some problems : is_home is returning TRUE, but this is not your homepage : it’s a WordPress page that lists every post, like a main category archive would do.

Use Conditional Tags

When you create a plugin or a theme, you have to consider that users may use those page settings. So, every time you use is_home, you should always test is_front_page before, and you always should use conditional parameters to do so.

If you don’t, you may have problems with some users. For example, if you only test is_home to display your homepage content, there will be issues with the static homepage that will not display the right content.

Here is how to do it right :

if ( is_front_page() && is_home() ){
	// Default homepage
} elseif ( is_front_page()){
	//Static homepage
} elseif ( is_home()){
	//Blog page
} else {
	//everything else
}

This is the only (right) way to display or alter content with your homepage and your blog page.

How About You?

Did you already encounter this problem? What was your solution? I want to hear about it!

Daniel Roch

Daniel Roch

SEO consultant at SeoMix
WordPress fanboy - SEO Addict. I love this CMS, and you can find a lot of french SEO posts about WordPress on my website.
Daniel Roch
Daniel Roch
This article has 6 comments
  1. AJ says:

    Thanks Daniel for sharing this guide. I’ve seen a lot of themes doing_it_wrong in the past – I’m sure I probably have done it in the past too when I was learning ;)

  2. Elmer says:

    Short, straight to the point, and very informative. Thank you for sharing!

  3. Matt Thiessen says:

    What if (is_front_page() && is_home()) still returns true when the URL is myblog.com/page/2/ since this isn’t really my homepage?

    1. AJ Clarke | WPExplorer says:

      It is actually still a part of your homepage. But you can use is_paged() to check if you are on a sub-page.

      Admin

Leave a Reply