WordPress at a glance

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().

  • Global. WP_Query. $wp_query Global WP_Query instance.
✈ 1 time = 0.001879s = very slow | 50000 times = 3.20s = fast PHP 7.1.2, WP 4.7.5

No Hooks.

Return

true/false. True, if front of site.

Usage

if( is_front_page() ) {
   // code
}

Examples

#1 A basic example of usage

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

#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' );

Code of is_front_page: wp-includes/query.php VER 4.9.8

<?php
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();
}

Related Functions

From tag: Conditional tags (page type and request)

More from tag: Conditional tags (all)

More from category: Queries

More from Template Tags: Main Functions

No comments
    Hello, !     Log In . Register