get_page_children()
最后更新于:2021-11-26 10:59:10
get_page_children( int$page_id, array$pages)Identify descendants of a given page ID in a list of page objects.
参数
- $page_id
-
(int) (Required) Page ID.
- $pages
-
(array) (Required) List of page objects from which descendants should be identified.
响应
(array) List of page children.
源文件
文件: gc-includes/post.php
function get_page_children( $page_id, $pages ) {
// Build a hash of ID -> children.
$children = array();
foreach ( (array) $pages as $page ) {
$children[ (int) $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;
}
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 GC to the browser (@bhlarsen's part :) ) echo '' . print_r( $inherited_locations, true ) . ''; ?>
<?php // Set up the objects needed $my_gc_query = new GC_Query(); $all_gc_pages = $my_gc_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_gc_pages ); // echo what we get back from GC to the browser echo '<pre>' . print_r( $portfolio_children, true ) . '</pre>'; ?>