WordPress at a glance

is_home() WP 1.5.0

The conditional tag that checks if the page with the last posts is displayed, usually it is the main page of the site or the blog homepage.

This function is very similar to is_front_page().

The operation of this function depends on the Reading Settings: Settings > Reading > Front page displays. By default, the main page displays the last posts of the site. By default, this tag will work for the main page of the site, but if you change the settings and specify a static page for the main page of the site, and another static page for the last posts, this tag will work for the page where the last posts are displayed and will stop working for the main page of the site.

In other words, if a static page is set for the front page of the site, this function will return true only on the page you set as the "Posts page".

The function also works on pagination pages, ex: /page/2.

✈ 1 time = 0.00005s = very fast | 50000 times = 0.02s = speed of light

No Hooks.

Return

true/false. True if blog view homepage, otherwise false.

Usage

is_home();

Usage

if( is_home() ){
   // code
}

Examples

#1 Displaying different content for the home page and others pages

For example, we want to display something in the sidebar for the main page and something other for the others pages:

if( is_home() ){
	echo 'Hello from the home (main) page.';
}
else {
	echo 'Hello from internal page.';
}

#2 Settings of "main page" and "posts page»

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_home: wp-includes/query.php VER 4.9.8

<?php
function is_home() {
	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_home();
}

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