Yogesh Chauhan's Blog

How to check if the page is the home page in WordPress?

in WordPress on February 9, 2021

We can use Conditional Tag is_home() to check if the page is the home page.

Quick Intro about Conditional Tags

Conditional Tags can be used in the Template Files to alter the display of content depending on the conditions that the current page matches.

Conditional Tags tell WordPress what code to display under specific conditions.

Conditional Tags usually work with PHP if /else Conditional Statements.

Things to remember before using it

For a Conditional Tag to modify your data, the information must already have been retrieved from your database, i.e. the query must have already run.

If you use a Conditional Tag before there is data, there’ll be nothing to ask the if/else statement about.

WordPress loads functions.php before the query is run, so if you simply include a Conditional Tag in that file, it won’t work.

The Main Page (Home page ) conditional tag

We use is_home() function to check if the page is the home page.

The blog homepage is the page that shows the time-based blog content of the site.

It returns true when the main blog page is being displayed.

If your home page has been set to a Static Page instead, then the function will only return true on the page on which you set as the “Posts page”.

You can change that settings from Settings > Reading.

Be careful not to confuse the two query conditionals:

On the site front page, is_front_page() will always return true, regardless of whether the site front page displays the blog posts index or a static page.

On the blog posts index, is_home() will always return true, regardless of whether the blog posts index is displayed on the site front page or a separate page.

Credit: WordPress

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Some interesting HTML Input Attributes to rememberHTMLHow to create a simple slider with CSS and jQuery?CSSWhat is an API?MiscellaneousMySQL queries to get data rows from previous month as well as from last 30 daysSQL/MySQLOrder By and Group By in PostgresPostgresKilling A Project Part 4: What Would Be Your Top 3 Priorities For Planning The Cancellation?Miscellaneous