A WordPress website can have different types of pages such as an information page, a contact page, a privacy policy page, an author page, or a home page.
Sometimes when we develop a theme, plugin or implement some other functionality in WordPress, it becomes important to determine if the current page is the home page. In this tutorial we will learn exactly how to do it.
What is WordPress home page and first page?
The homepage is a special type of page in WordPress. Refers to a website page that contains the list of all blog posts you have published in chronological order.
This is not to be confused with the static first page concept introduced in WordPress 2.1. The content of a static first page will not necessarily be static. However, it doesn’t mean containing a list of all blog posts like the home page.
The appropriate options for setting a static page as the first page can be found by visiting Settings> Reading from browsing the WordPress admin dashboard.
The terminology for the home page and a front page can be a bit confusing. Typically, we refer to a website’s main URL as its home page. For example, it will be the Tuts + home page https://tutsplus.com/. However, this is considered the first page in WordPress.
The home page and first page of a WordPress website can be the same or different from each other depending on your settings.
Using the is_home()
And is_front_page()
functions in WordPress
You can use the is_home()
function in WordPress to determine if the current page is the blog home page or the page that lists all blog posts. It will show all blog posts on your website in chronological order.
Using the screenshot in the previous section as a reference, the is_home()
function will return true for the URL website.com/blog/. The First page the option in the settings refers to the home page of the blog and not the main URL of the website.
You have to use the is_front_page()
function to determine if you are currently on the front page or the main URL of the website such as website.com. In our example, the content of the first page or main website URL will be the same as the store page.
Any confusion regarding these two functions can be avoided by keeping in mind the following points:
The function is_front_page()
will always come back true
when you are on the main URL of the website such as website.com. It doesn’t matter if you use the main URL to view some static content or a list of blog posts. The function is_front_page()
will come back false
on all other pages.
The function is_home()
will always come back true
when you are on a page showing the blog post index. It doesn’t matter if it’s the first page of the website or some other page. Calling is_home()
elsewhere it will always return false
.
Using is_home()
And is_front_page()
together
You may need to use is_home()
And is_front_page()
together to perform certain tasks depending on the settings.
Let’s say you have chosen the reading settings in your WordPress dashboard such that the home page displays the latest blog posts as shown in the image above. In this case, the value of is_home()
And is_front_page()
Sara true
for the main website URL such as website.com.
<?php // When checking the main website URL with the homepage set to display Latest Posts. if(is_home()) { // Executes } if(is_front_page()) { // Executes } if(is_home() && is_front_page()) { // Executes } if(is_home() || is_front_page()) { // Executes } ?>
As you can see, using is_front_page()
it is no different from using is_home()
in this situation. They will both return true
for the main URL and false
for all other URLs.
Now, let’s say you have set up a static page titled “Products” with the URL website.com/products/ as the first page of the website and a blog page with the URL website.com/blog/ such as blog home page or blog index page.
<?php // When checking the main website URL with the homepage set to be a static page if(is_home()) { // Doesn't execute } if(is_front_page()) { // Executes } if(is_home() && is_front_page()) { // Doesn't Execute } if(is_home() || is_front_page()) { // Executes } ?>
The list of blog posts will be shown on the page website.com/blog/. In this case, only the code inside the first and last if
the blocks will be executed.
<?php // When checking the URL website.com/blog/ with blog set to be the posts page. if(is_home()) { // Executes } if(is_front_page()) { // Doesn't execute } if(is_home() && is_front_page()) { // Doesn't Execute } if(is_home() || is_front_page()) { // Executes } ?>
None of these if
blocks will be executed for any other page on the site.
Let’s say you’ve set your custom page as your blog home page or the page where all the most recent posts will appear. What if you decide to display the current page title before the post list but only if you are not in the main website URL? In that case, you can use the following conditional block:
<?php if(is_home() && !is_front_page()) { echo '<h1>'.single_post_title().'</h1>'; } ?>
Programmatic control of user preferences
It is also possible for us to carry out checks on user preferences regarding the setting of a specific home page or front page. We can use the get_option()
function to perform this check.
There are three options whose values we can control. These are: show_on_front
, page_on_front
And page_for_posts
.
The value returned from a call to get_option('show_on_front')
it would be either posts
or page
.
When this function call comes back posts
, indicates that the first page has been set to show the list of the latest blog posts. In other words, it means that the calls to is_home()
And is_front_page()
they will both come back true
.
When the call to get_option('show_on_front')
come back page
, it means that a static page has been assigned as the first page of the website. A call to get_option('page_on_front')
now it will give you the id of that static page. You can also make a call to get_option('page_for_posts')
to get the static page ID to use for viewing the latest blog posts.
Here is the output of these three calls from my website. Once you have the ID of the static pages, you can access more information about them.
<?php // page echo get_option('show_on_front') // 342 echo get_option('page_on_front') // 134 echo get_option('page_for_posts') ?>
Final thoughts
In this tutorial, we learned about two important functions in WordPress called is_home()
And is_front_page()
which we can use to check if we are currently on the blog post page or the front page of the website. We learned the distinction between these two types of pages and how running checks with both of these functions can help us execute certain code in very specific situations.
Eventually, we also learned how to determine which page of the website was set to be the first page or blog post page.
Thumbnail of the post generated by OpenAI DALL-E.