在页面对象列表中标识给定页面ID的后代。

原型

get_page_children( int $page_id, array $pages )

描述

从传递给函数的$ pages数组中识别后代。不执行任何数据库查询。

参数

$page_id (int) (Required) 页面ID。

$pages (array) (Required) 应标识后代的页面对象列表。

返回值

(array)  页面儿童列表。

源文件

路径:wp-includes/post.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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;
}

...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/get_page_children/