get_page_hierarchy()对一组页面进行排序

目录

描述

Order the pages with children under parents in a flat list.

get_page_hierarchy() 描述

用法

<?php get_page_hierarchy$posts$parent ?>

get_page_hierarchy() 用法

参数

$posts

(array) (必填) Posts array.

默认值: None

$parent

(integer) (可选) Parent page ID.

默认值: 0

get_page_hierarchy() 参数

返回值

(array) 

A list arranged by hierarchy. Children immediately follow their parents.

get_page_hierarchy() 返回值

历史

添加于 版本: 2.0.0

get_page_hierarchy() 历史

源文件

get_page_hierarchy() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Order the pages with children under parents in a flat list.
 *
 * It uses auxiliary structure to hold parent-children relationships and
 * runs in O(N) complexity
 *
 * @since 2.0.0
 *
 * @param array $pages   Posts array, passed by reference.
 * @param int   $page_id Optional. Parent page ID. Default 0.
 * @return array A list arranged by hierarchy. Children immediately follow their parents.
 */
function get_page_hierarchy( &$pages, $page_id = 0 ) {
	if ( empty( $pages ) ) {
		return array();
	}

	$children = array();
	foreach ( (array) $pages as $p ) {
		$parent_id = intval( $p->post_parent );
		$children[ $parent_id ][] = $p;
	}

	$result = array();
	_page_traverse_name( $page_id, $children, $result );

	return $result;
}

get_page_hierarchy() 源文件

相关