is_page( int|string|array $page = '' )
Determines whether the query is for an existing single page.
Contents
Description Description
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.
See also See also
Parameters Parameters
- $page
-
(int|string|array) (Optional) Page ID, title, slug, or array of such.
Default value: ''
Return Return
(bool) Whether the query is for an existing single page.
Source Source
File: wp-includes/query.php
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 ); }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
1.5.0 | Introduced. |
More Information More Information
Notes Notes
- Will return true if an empty value is passed
- Due to certain global variables being overwritten during The Loop, is_page() will not work. In order to call it after The Loop, you must call wp_reset_query() first.
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Expand full source codeCollapse full source code
is_page()
supports array too:Testing for paginated Pages
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
Example 2
Testing for sub-Pages
There is no
is_subpage()
function yet, but you can test this with a little code:Snippet 1
You can create your own
is_subpage()
function using the code in Snippet 2. Add it to yourfunctions.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
Expand full source codeCollapse full source code
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
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
Expand full source codeCollapse full source code
Add Snippet 4 to your
functions.php
file, and callis_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.
Use it only after ‘setup_theme’ hook i fired. Otherwise it will return false.
Testing for Custom Post Type Sub-Pages
is_page()
can be used in the following ways: