How to detect if the current page is the home page in a WordPress theme

In a WordPress theme, you can use the is_front_page() and is_home() conditional functions to detect if the current page is the home page.

  • is_front_page(): Returns true if the current page is the front page, which can be either the latest posts or a static page depending on the settings in the WordPress admin dashboard.
  • is_home(): Returns true if the current page is the home page showing the latest posts. If the front page is set to a static page, is_home() will return true for the blog page.

Here’s how to use these functions:

if (is_front_page() && is_home()) {
    // The home page is displaying the latest posts
    echo 'This is the home page showing the latest posts.';
} elseif (is_front_page()) {
    // The home page is a static page
    echo 'This is the home page set to a static page.';
} else {
    // This is not the home page
    echo 'This is not the home page.';
}

You can add this code snippet to your WordPress theme’s template files to display different content or apply specific styling depending on whether the current page is the home page or not.

Remember to always backup your files before making any changes, and test your changes on a staging site or local development environment before applying them to your live site.

Leave a Reply

Your email address will not be published. Required fields are marked *