get_page_children

函数
get_page_children ( $page_id, $pages )
参数
  • (int) $page_id Page ID.
    Required:
  • (WP_Post[]) $pages List of page objects from which descendants should be identified.
    Required:
返回值
  • (WP_Post[]) List of page children.
定义位置
相关方法
get_childrenget_term_children_get_term_childrenget_category_childrenget_page_link
引入
1.5.1
弃用
-

get_page_children函数是一个WordPress的函数,用于检索一个父页面的子页面: 这个函数接受两个参数:父页面的ID和一个控制输出的参数阵列: 该函数返回一个子页面对象的数组。

识别页面对象列表中给定页面ID的后裔。

后裔是通过传递给函数的`$pages`数组来识别的。不进行数据库查询。

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;
}

常见问题

FAQs
查看更多 >