A WordPress website can have different kinds of pages like an about page, contact page, privacy policy page, author page, or homepage.
Sometimes, when we are developing a theme, a plugin or implementing some other functionality in WordPress, it becomes important to determine whether the current page is the homepage. In this tutorial, we will learn exactly how to do that.
What is WordPress Homepage and Front Page?
The homepage is a special type of page in WordPress. It refers to a page on the website that contains the list of all blog posts that you have published in a chronological order.
This is not to be confused with the concept of a static front page which was introduced in WordPress 2.1. The content of a static front page is not necessarily going to be static. However, it is not meant contain a list of all blog posts like the homepage.
The appropriate options for setting a static page as the front page can be found by visiting Settings > Reading from the navigation in WordPress admin dashboard.
The terminology for home page and a front page can be a bit confusing. Generally, we refer to the main URL of a website as its homepage. For example, the homepage for Tuts+ will be https://tutsplus.com/. However, this is considered the front page in WordPress.
The homepage and the front page a WordPress website can either be the same or different from each other depending on the 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 homepage or the page which lists all blog posts. It will show all the blog posts of your website in chronological order.
Using the screenshot in previous section as reference, the is_home()
function will return true for the URL website.com/blog/. The Homepage option in the settings refers to the blog homepage and not the main URL of the website.
You need to use the is_front_page()
function to determine whether you are currently at the front page or the main URL on the website like website.com. In our example, the content for the front page or the main website URL will be same as the shop page.
You can avoid any confusion regarding these two functions by keeping the following points in mind:
The function is_front_page()
will always return true
when you are on the main URL of the website like website.com. It doesn't matter if you use the main URL to display some static content or a list of blog posts. The function is_front_page()
will return false
on all other pages.
The function is_home()
will always return true
when you are on a page that displays the blog posts index. It doesn't matter if it is the front page of the website or some other page. Calling is_home()
anywhere else will always return false
.
Using is_home()
and is_front_page()
together
You might need to use is_home()
and is_front_page()
together to accomplish certain tasks depending on your settings.
Lets say that you have chosen the reading settings in WordPress dashboard such that the homepage displays the latest blog posts as shown in the image above. In this case, the value of is_home()
and is_front_page()
will be true
for the main URL of the website like 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()
is no different than using is_home()
in this situation. Both of them will return true
for the main URL and false
for all other URLs.
Now, lets say you have set a static page titled "Products" with the URL website.com/products/ as the front page of the website and a blog page with the URL website.com/blog/ as the blog homepage or the 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
blocks will execute.
<?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 execute for any other pages of the website.
Lets say you have set your own custom page as the blog homepage or the page where all the latest posts will appear. What if you decide to display the title of the current page before the list of posts but only if you are not on 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>'; } ?>
Programmatically Checking User Preferences
It is also possible for us to perform checks about user preference related to the setting of a specific homepage or front page. We can use the get_option()
function to do this check.
There are three options whose values we can check. These are: show_on_front
, page_on_front
, and page_for_posts
.
The value returned by a call to get_option('show_on_front')
would either be posts
or page
.
When this function call returns posts
, it indicates that the front page has been set up to show the list of latest blog posts. In other words, it means that the calls to is_home()
and is_front_page()
will both return true
.
When the call to get_option('show_on_front')
returns page
, it means that a static page has been assigned to be the front page of the website. A call to get_option('page_on_front')
will now give you the ID of that static page. You can also make a call to get_option('page_for_posts')
in order to get the ID of static page that is to be used for displaying the latest blog posts.
Here is the output for these three calls from my website. Once you have the ID of the static pages, you can get access to 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()
that we can use to check if we are currently on the blog posts page or the front page of the website. We learned the distinction between these two types of pages and how performing checks with both these functions can help us execute certain code in very specific situations.
At the end, we also learned how to determine which page on the website has been set to be the front page or the blog posts page.
Post thumbnail generated by OpenAI DALL-E.
No comments:
Post a Comment