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

is_page() 描述


<?php is_page($page); ?>

is_page() 用法



(mixed) (可选) Page ID, Page Title or Page Slug

默认值: None

is_page() 参数



true on success, false on failure.

is_page() 返回值


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

Example 3

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 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

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

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.

Is a Page Template

Allows you to determine whether or not you are in a page template or if a specific page template is being used.


Is a Page Template being used?

is_page_template( 'about.php' ) 

Is Page Template 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php. Note: if the file is in a subdirectory you must include this as well. Meaning that this should be the filepath in relation to your theme as well as the filename, for example 'page-templates/about.php'.

is_page() 示例


Passing Empty Value Returns TRUE

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:

Cannot Be Used Inside The Loop

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.

is_page() 注意


添加于 版本: 1.5.0

is_page() 历史


is_page() 函数的代码位于 wp-includes/query.php.

is_page() 源文件