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

wordpress函数get_page_children()用法示例

get_page_children( int $page_id,  array $pages )

Identify descendants of a given page ID in a list of page objects.


�述

Descendants are identified from the $pages array passed to the function. No database queries are performed.

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


�数

$page_id

(int)
(Required)
Page ID.

$pages

(array)
(Required)
List of page objects from which descendants should be identified.


返�值

(array) List of page children.


�代�

File: wp-includes/post.php

function get_page_children( $page_id, $pages ) {
	// Build a hash of ID -> children.
	$children = array();
	foreach ( (array) $pages as $page ) {
		$children[ intval( $page->post_parent ) ][] = $page;
	}

	$page_list = array();

	// Start the search by looking at immediate children.
	if ( isset( $children[ $page_id ] ) ) {
		// Always start at the end of the stack in order to preserve original `$pages` order.
		$to_look = array_reverse( $children[ $page_id ] );

		while ( $to_look ) {
			$p = array_pop( $to_look );
			$page_list[] = $p;
			if ( isset( $children[ $p->ID ] ) ) {
				foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
					// Append to the `$to_look` stack to descend the tree.
					$to_look[] = $child;
				}
			}
		}
	}

	return $page_list;
}

更新日志

Version �述
1.5.1 Introduced.

相关函数

Used By

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

  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

    Examples

    
    <?php
    // Set up the objects needed
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
    
    // Get the page as an Object
    $portfolio =  get_page_by_title('Portfolio');
    
    // Filter through all pages and find Portfolio's children
    $portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );
    
    // echo what we get back from WP to the browser
    echo '<pre>' . print_r( $portfolio_children, true ) . '</pre>';
    ?>
    
  2. In one of my Hierarchical Custom Post Type (locations) I did @bhlarsen’s method, and in some extent it’s returning false children. So I did it my way:

    ID;

    //Instead of calling and passing query parameter differently, we’re doing it exclusively
    $all_locations = get_pages( array(
    ‘post_type’ => ‘locations’, //here’s my CPT
    ‘post_status’ => array( ‘publish’, ‘pending’ ) //my custom choice
    ) );

    //Using the function
    $inherited_locations = get_page_children( $location_parent_id, $all_locations );

    // echo what we get back from WP to the browser (@bhlarsen’s part 🙂 )
    echo ” . print_r( $inherited_locations, true ) . ”;
    ?>

    It’s giving me the correct children.

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

热门推荐

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

反馈