get_page_children()
get_page_children( int $page_id, array $pages ) 在页面对象列表…
get_page_children( int $page_id, array $pages )
在页面对象列表中标识给定页面ID的后代。
Identify descendants of a given page ID in a list of page objects.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
子体从传递给函数的$pages数组中标识。不执行数据库查询。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $page_id | (int) | 必需 | 页面ID。 |
| $pages | (array) | 必需 | 应从中标识子代的页对象列表。 |
返回(Return)
(array)页子级列表。
源码(Source)
/**
* 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.
*
* @since 1.5.1
*
* @param int $page_id Page ID.
* @param array $pages List of page objects from which descendants should be identified.
* @return array List of page children.
*/
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;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 1.5.1 | wp-includes/post.php:5070 | 1 function | 0 |
笔记(Notes)
在我的一个分层自定义Post类型(位置)中,我使用了@bhlarsen的方法,并且在某种程度上返回了假子级。所以我按自己的方式做了:
实例
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!