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`陣列來識別的。不進行資料庫查詢。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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
檢視更多 >