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 inSettings=> Reading.
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 :
1 | if ( is_front_page() && is_home() ){ |
2 | // Default homepage |
3 | } elseif ( is_front_page()){ |
4 | //Static homepage |
5 | } elseif ( is_home()){ |
6 | //Blog page |
7 | } else { |
8 | //everything else |
9 | } |
This is the only (right) way to display or alter content with your homepage and your blog page.
0 comments:
Post a Comment