is_front_page()WP 2.5.0

Checks whether the main (home) page of the site is displayed. Conditional tag.

This conditional tag will work in any case: whether it is a page with the output of the last posts or a page where the main page of the site is set to a static page.

When static page is set for the front page (home, front page) is_home() no longer works on the front page, but works: is_front_page(), is_singular(), is_page().

In the settings (Settings > Reading > Front page displays) you can set a static page for the main page and a static page to display the last posts. is_front_page() will work for the main page of the site anyway, but is_home() will work only for the page with the output of the last posts.

On pagination pages, this conditional tag will also work. /page/2 will be considered as the main page (front page).

See similar function is_home().

1 time — 0.001879 sec (very slow) | 50000 times — 3.20 sec (fast) | PHP 7.1.2, WP 4.7.5

No Hooks.

Return

true|false. Whether the query is for the front page of the site.

Usage

if( is_front_page() ) {
   // code
}

Examples

0

#1 A basic example of usage

if( is_front_page() ){
	 echo "This is the main page";
}
else {
	 echo "This is not the main page";
}
0

#2 Options names for "front page" and "page of posts"

Options where page IDs are stored, which are set for the "home page" and "posts page":

// The ID of the static page that is listed as the main page of the site
get_option( 'page_on_front' );

// The ID of the static page that is listed as blog page (the latest posts of the site)
get_option( 'page_for_posts' );

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 2.5.0 Introduced.

is_front_page() code WP 6.4.3

function is_front_page() {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_front_page();
}