is_page()
最后更新于:2021-11-27 09:37:24
is_page( int|string|int[]|string[]$page=”)Determines whether the query is for an existing single page.
参数
- $page
-
(int|string|int[]|string[]) (Optional) Page ID, title, slug, or array of such to check against.
Default value: ”
响应
(bool) Whether the query is for an existing single page.
源文件
文件: gc-includes/query.php
function is_page( $page = '' ) {
global $gc_query;
if ( ! isset( $gc_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 $gc_query->is_page( $page );
}
// When any single Page is being displayed. is_page(); // When Page 42 (ID) is being displayed. is_page( 42 ); // When the Page with a post_title of "Contact" is being displayed. is_page( 'Contact' ); // When the Page with a post_name (slug) of "about-me" is being displayed. is_page( 'about-me' ); /* * 响应s 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 in version 2.5. */ is_page( array( 42, 'about-me', 'Contact' ) );
/** * Check whether we are on a subpage * * @return mixed ID of the parent post or false if this is not a subpage. */ function gcdocs_is_subpage() { // Load details about this page. $post = get_post(); // test to see if the page has a parent if ( is_page() && $post->post_parent ) { // 响应 the ID of the parent post. return $post->post_parent; // There is no parent so ... } else { // ... The answer to the question is false return false; } }
/** * Check whether we are on this page or a sub page * * @param int $pid Page ID to check against. * @return bool True if we are on this page or a sub page of this page. */ function gcdocs_is_tree( $pid ) { // $pid = The ID of the page we're looking for pages underneath $post = get_post(); // load details about this page $is_tree = false; if ( is_page( $pid ) ) { $is_tree = 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 ) { $is_tree = true; } } return $is_tree; // we arn't at the page, and the page is not an ancestor }