^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_post_ancestors()用法示例

get_post_ancestors( int|WP_Post $post )

Retrieve ancestors of a post.


描述


参数

$post

(int|WP_Post)
(Required)
Post ID or post object.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


返回值

(array) Ancestor IDs or empty array if none are found.


源代码

File: wp-includes/post.php

function get_post_ancestors( $post ) {
	$post = get_post( $post );

	if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID )
		return array();

	$ancestors = array();

	$id = $ancestors[] = $post->post_parent;

	while ( $ancestor = get_post( $id ) ) {
		// Loop detection: If the ancestor has been seen before, break.
		if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
			break;

		$id = $ancestors[] = $ancestor->post_parent;
	}

	return $ancestors;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-includes/post.php:
    get_post()

Used By

  • wp-admin/includes/class-wp-posts-list-table.php:
    WP_Posts_List_Table::single_row()
  • wp-includes/taxonomy.php:
    get_ancestors()
  • wp-includes/class-wp-post.php:
    WP_Post::__get()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Get Ancestors Page Slug
    This example returns the highest page {slug} in a tree and uses it as a Body_Class, so the parent and all children will have the same Body Class!

    This example for a twenty eleven child theme in the header.php file

    
    </head>
    
    <?php
    /* Get the Page Slug to Use as a Body Class, this will only return a value on pages! */
    $class = '';
    /* is it a page */
    if( is_page() ) { 
    	global $post;
    	/* Get an array of Ancestors and Parents if they exist */
    	$parents = get_post_ancestors( $post->ID );
    	/* Get the top Level page->ID count base 1, array base 0 so -1 */ 
    	$id = ($parents) ? $parents[count($parents)-1]: $post->ID;
    	/* Get the parent and set the $class with the page slug (post_name) */
    	$parent = get_post( $id );
    	$class = $parent->post_name;
    }
    ?>
    
    <body <?php body_class( $class ); ?>>
    
    
  2. Get Ancestors Post Meta
    If we did not want to use the page slug, we could use a custom field eg: body_class, on the top level page and set the class in the post meta.

    </head>
    
    <?php
    $class = '';
    if( is_page() ) {
    	global $post;
    	$parents = get_post_ancestors( $post->ID );
    	$id = ($parents) ? $parents[count($parents)-1]: $post->ID;
    	$class = get_post_meta( $id, 'body_class', true );
    }
    ?>
    
    <body <?php body_class( $class ); ?>>
    
    

    Get Ancestors Page Thumbnail
    Get the top level page thumbnail and display it!

    
    <?php
    global $post;
    $parents = get_post_ancestors( $post->ID );
    /* Get the ID of the 'top most' Page if not return current page ID */
    $id = ($parents) ? $parents[count($parents)-1]: $post->ID;
    if(has_post_thumbnail( $id )) {
    	get_the_post_thumbnail( $id, 'thumbnail');
    }
    ?>
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈