返回介绍

is_page()

发布于 2017-09-11 01:21:17 字数 8622 浏览 1099 评论 0 收藏 0

is_page( int|string|array $page = '' )

Is the query for an existing single page?


description

If the $page parameter is specified, this function will additionally check if the query is for one of the pages specified.


参数

$page

(int|string|array) (Optional) Page ID, title, slug, or array of such.

Default value: ''


返回值

(bool) Whether the query is for an existing single page.


源代码

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 );
}

更新日志

Versiondescription
1.5.0Introduced.

More Information

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.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/class-wp-query.php: WP_Query::is_page()
  • wp-includes/functions.php: _doing_it_wrong()

Used By

  • wp-includes/general-template.php: wp_title()
  • wp-includes/post-template.php: wp_list_pages()
  • wp-includes/post-template.php: get_body_class()
  • wp-includes/canonical.php: redirect_canonical()
  • wp-includes/comment-template.php: comments_template()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by Mayeenul Islam

    is_page() supports array too:

    
    if( is_page( array( 'about-us', 'contact', 'management' ) )
         //either in about us, or contact, or management page is in view
    else
         //none of the page about us, contact or management is in view
    
  2. 
    // 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' );
    
    /*
     * 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 in version 2.5.
     */
    is_page( array( 42, 'about-me', 'Contact' ) );
    

    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

    
    <?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.
    }
    ?>
    

    Testing for sub-Pages

    There is no is_subpage() function yet, but you can test this with a little code:

    Snippet 1

    
    // If outside the loop.
    $post = get_post();
    
    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

    
    /**
     * Check whether we are on a subpage
     *
     * @return mixed ID of the parent post or false if this is not a subpage.
     */
    function wpdocs_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 ) {
    		// Return 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;
    	}
    }
    

    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

    
    /**
     * 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 wpdocs_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
    }
    

    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.

    Testing for Custom Post Type Sub-Pages

    
    $page = get_post();
    
    if ( is_singular('Custom_Post_Type_Name_Here') && $page->post_parent > 0 ) {
        // This is a custom post type subpage
    } else {
        // This is not a custom post type subpage
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文