is_page()WP 1.5.0

Checks whether a "static page" is displaying. You can specify an ID, name, or page title to check a specific page. Conditional tag.

If the $page parameter is specified, this function will additionally check if the query is for one of the pages specified.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Will return true when passing null values

Be attentive as the following calls of this function will return true:

is_page( '' )
is_page( 0 )
is_page( '0' )
is_page( null )
is_page( false )
is_page( array() )

Cannot be used inside a WordPress Loop

Because some global variables are overwritten in the loop, is_page() cannot be used inside the loop. However, this is not necessary. To use this template Tag after the Loop, you need reset an arbitrary loop (a request to get other posts) with the function: wp_reset_query().

Used By: is_shop()
1 time — 0.000012 sec (very fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.8, WP 4.6.1

No Hooks.

Return

true|false.

  • true if the page post type is displayed: static page
  • false if any other post type is displayed.

Usage

if( is_page( $page ) ){
	 // code
}
$page(int/string/array)

Page ID, title, slug, or array of such.

You can also specify the full path for nested pages: parent-page/child-page/. Then the function will try to get the page via get_page_by_path() and compare whether the specified $page is the current page.

Default: '' (any page)

Examples

1

#1 Check whether we are on a child page of a static page

There is no function is_subpage() in WordPress. But such a check can be done with the following code:

Example 1

global $post;     // If outside the cycle

if ( is_page() && $post->post_parent ) {
	// This is a child page

} else {
	// This is not a child page
}

Example 2

You can create your own is_subpage() function. Add the code below to the theme's functions.php file. It works like the first example: checks whether the current page is a child page, i.e. whether it is a page post_type and whether it has a parent page.

/*
 * Check whether the current static page is a child page
 * @return true/false
 */
function is_subpage() {
	global $post;

	if ( is_page() && $post->post_parent ) {
		return $post->post_parent;
	} 
	return false;
}

Example 3

Use this example to determine the About page or its child page. This example is useful if you want to specify a variable for the entire page branch. Here we, for example, indicate a picture for the all pages branch:

if ( is_page('about') || '2' == $post->post_parent ) {    
	// This is the "about" page or its child page
	$bannerimg = 'about.jpg';

}
elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) {    
	$bannerimg = 'teaching.jpg';

}
elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { 
	$bannerimg = 'admissions.jpg';

}
// if we are on an undefined page, display the default image
else { 
	$bannerimg = 'home.jpg';
}

Example 4

Create a function that will check all levels of nesting, i.e. the entire branch of subpages and if we are on one of the pages of the branch, the function will return true, otherwise false. Let's suppose we have a page "About us" it has a child page "Our services", and this page has another child pages "Painting" and "Finishing". In this function, we pass the ID of the page "About us" and it will return true if we are on any of the mentioned pages:

/*
 * Check the child pages at all nesting levels
 * @param (int) $pid   Page ID all levels of child pages we need to check
 */
function is_tree( $pid ){
	global $post;

	// return if we are already on the specified page
	if( is_page( $pid ) )
		return true;

	$anc = get_post_ancestors( $post->ID );
	foreach( $anc as $ancestor ){
		if( is_page() && $ancestor == $pid ){
			return true;
		}
	}

	return false;
}
0

#2 Function in action

Various examples of use - cases when the function returns true (function triggered):

// when any static page is displayed.
is_page();

// when the page with ID 42 is displayed.
is_page(42);

// When the page with the title "About site" is displayed.
is_page('About site');

// When the page with the name "about-site" is displayed.
is_page('about-site');

// Parameters can be combined.
is_page( [ 42, 'about-site', 'About site' ] );

// You can check multiple pages at the same time. All specified pages are different...
is_page( [ 'sample-page', 'contacts', 'parent-slug/page-name', '/parent-slug/page-name/', 23, 34 ] );
0

#3 Page separation check

We can use the shortcode <!--nextpage--> inside the post content. This code will divide the text of the post into several pages. The code below is useful when you want to display some data on the first of multiple pages only.

Example 1

$paged = $wp_query->get( 'paged' );

if ( ! $paged || $paged < 2 ){
	// The post is not divided into pages or it is not the first page.
}
else {
	// That's 2,3,4 ... page of the divided post.
}

Example 2

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : false;
if ( $paged === false ){
	// The post is not divided into pages or it is not the first page.
}
else {
	// That's 2,3,4 ... page of the divided post.

}

Example 3

if ( 0 === get_query_var( 'page' ) ) {
	// The post is not divided into pages or it is not the first page.
}
else {
	// That's 2,3,4 ... page of the divided post.
}
0

#4 A note about the Cyrillic and name (slug)

If your website does not change the Cyrillic to the Latin alphabet - plugin Сry to Lat (or similar) is not installed, when a post is created its name changes and Cyrillic transforms to special symbols (ex: contacts will be changed to %d0%ba%d0%be%d0%bd%d1%82%d0%b0%d0%ba%d1%82%d1%8b), so when checking you need to take this into account. I.e. if the name (post_name) is checked instead of the title, do so:

is_page('о-сайте'); // wrong

is_page( sanitize_title('о-сайте') ); // correct

Notes

Changelog

Since 1.5.0 Introduced.

is_page() code WP 6.4.3

function is_page( $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_page( $page );
}