This page redirects to an external site: https://developer.wordpress.org/reference/functions/is_page/
Languages: English • 日本語 (Add your language)
This Conditional Tag checks if Pages are being displayed. This is a boolean function, meaning it returns either TRUE or FALSE. This tag must be used BEFORE The Loop and does not work inside The Loop (see Notes below).
<?php is_page($page); ?>
is_page(); // When any single Page is being displayed. is_page( 42 ); // When Page 42 (ID) is being displayed. is_page( 'Contact' ); // When the Page with a post_title of "Contact" is being displayed. is_page( 'about-me' ); // When the Page with a post_name (slug) of "about-me" is being displayed. is_page( array( 42, 'about-me', 'Contact' ) ); // Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "Contact". Note: the array ability was added at Version 2.5.
You can use this code to check whether you're on the nth page in a Post or PAGE Page that has been divided into pages using the <!--nextpage--> QuickTag. This can be useful, for example, if you wish to display meta-data only on the first page of a post divided into several pages.
Example 1
<?php
$paged = $wp_query->get( 'paged' );
if ( ! $paged || $paged < 2 )
{
// This is not a paginated page (or it's simply the first page of a paginated page/post)
}
else
{
// This is a paginated page.
}
?>
Example 2
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : false;
if ( $paged === false )
{
// This is not a paginated page (or it's simply the first page of a paginated page/post)
}
else
{
// This is a paginated page.
}
?>
Example 3
<?php
if ( 0 === get_query_var( 'page' ) ) {
// This is not a paginated page (or it's simply the first page of a paginated page/post)
}
else {
// This is a paginated page.
}
There is no is_subpage() function yet, but you can test this with a little code:
Snippet 1
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
You can create your own is_subpage() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or false if there isn't.
Snippet 2
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
return $post->post_parent; // return the ID of the parent post
} else { // there is no parent so ...
return false; // ... the answer to the question is false
}
}
It is advisable to use a function like that in Snippet 2, rather than using the simple test like Snippet 1, if you plan to test for sub pages frequently.
To test if the parent of a page is a specific page, for instance "About" (page id pid 2 by default), we can use the tests in Snippet 3. These tests check to see if we are looking at the page in question, as well as if we are looking at any child pages. This is useful for setting variables specific to different sections of a web site, so a different banner image, or a different heading.
Snippet 3
<?php
if ( is_page( 'about' ) || '2' == $post->post_parent ) {
// the page is "About", or the parent of the page is "About"
$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';
} else {
$bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
}
?>
Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looking at the page in question (so "About") or one of its sub pages (so a page with a parent with ID "2").
Snippet 4
function is_tree( $pid ) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if ( is_page($pid) )
return true; // we're at the page or at a sub page
$anc = get_post_ancestors( $post->ID );
foreach ( $anc as $ancestor ) {
if( is_page() && $ancestor == $pid ) {
return true;
}
}
return false; // we arn't at the page, and the page is not an ancestor
}
Add Snippet 4 to your functions.php file, and call is_tree( 'id' ) to see if the current page is the page, or is a sub page of the page. In Snippet 3, is_tree( '2' ) would replace "is_page( 'about' ) || '2' == $post->post_parent" inside the first if tag.
Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy.
Allows you to determine whether or not you are in a page template or if a specific page template is being used.
Be very careful if there's a possibility of passing an empty value as a parameter to check for a specific page, since the following lines will return true:
is_page( '' ) is_page( 0 ) is_page( '0' ) is_page( null ) is_page( false ) is_page( array() )
Due to certain global variables being overwritten during The Loop is_page() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop.
Since: 1.5.0
is_page() is located in wp-includes/query.php.