【描述】
用于判断当前页面是否被显示.布尔型函数,返回 TRUE 或者 FALSE. 这个方法必须在loop循环前使用,并且 在Loop循环中不能使用.
【使用】
- <?php is_page($page); ?>
【参数】
$page
(混合型) (optional) 页面 ID, 页面 Title or 页面 Slug
默认: None
返回值
(boolean)
成功返回true,失败返回 false.
【例子】
- is_page();
- // 当任何页面被显示.
- is_page(42);
- // 当页面id是42被显示.
- is_page('Contact');
- // 当标题post_title 是 "Contact"的页面被显示.
- is_page('about-me');
- // 当别名post_name (slug) 是 "about-me" 的页面被显示.
- is_page(array(42,'about-me','Contact'));
- // 当 post ID 42, 或者 post_name 是 "about-me", 或者 post_title 是 "Contact". 返回 true 注意: 数组变量在 版本 2.5 添加.
【注意】
传入一下空变量将会返回true
- is_page( '' )
- is_page( 0 )
- is_page( '0' )
- is_page( null )
- is_page( false )
- is_page( array() )■See also: is_singular()
不能在Loop循环内使用
如果在Loop循环后使用必须先调用 wp_reset_query() .
【源文件】
is_page() 位于 wp-includes/query.php.
- /**
- * Is the query for a single page?
- *
- * If the $page parameter is specified, this function will additionally
- * check if the query is for one of the pages specified.
- *
- * @see WP_Query::is_single()
- * @see WP_Query::is_singular()
- *
- * @since 3.1.0
- *
- * @param mixed $page Page ID, title, slug, or array of such.
- * @return bool
- */
- function is_page( $page = '' ) {
- if ( !$this->is_page )
- return false;
- if ( empty( $page ) )
- return true;
- $page_obj = $this->get_queried_object();
- $page = (array) $page;
- if ( in_array( $page_obj->ID, $page ) )
- return true;
- elseif ( in_array( $page_obj->post_title, $page ) )
- return true;
- else if ( in_array( $page_obj->post_name, $page ) )
- return true;
- return false;
- }