这是一个将输出顶级父级以及当前页面的所有子级的函数。将有用的菜单类添加到输出中。E、 g。page_item_has_children
, current_page_item
, current_page_ancestor
, 等
此解决方案是based on one of the examples 在文档中wp_list_pages()
和this answer 这里是WPSE,它引用了相同的示例。
/**
* Use wp_list_pages() to display parent and all child pages of current page.
*/
function wpse_get_ancestor_tree() {
// Bail if this is not a page.
if ( ! is_page() ) {
return false;
}
// Get the current post.
$post = get_post();
/**
* Get array of post ancestor IDs.
* Note: The direct parent is returned as the first value in the array.
* The highest level ancestor is returned as the last value in the array.
* See https://codex.wordpress.org/Function_Reference/get_post_ancestors
*/
$ancestors = get_post_ancestors( $post->ID );
// If there are ancestors, get the top level parent.
// Otherwise use the current post\'s ID.
$parent = ( ! empty( $ancestors ) ) ? array_pop( $ancestors ) : $post->ID;
// Get all pages that are a child of $parent.
$pages = get_pages( [
\'child_of\' => $parent,
] );
// Bail if there are no results.
if ( ! $pages ) {
return false;
}
// Store array of page IDs to include latere on.
$page_ids = array();
foreach ( $pages as $page ) {
$page_ids[] = $page->ID;
}
// Add parent page to beginning of $page_ids array.
array_unshift( $page_ids, $parent );
// Get the output and return results if they exist.
$output = wp_list_pages( [
\'include\' => $page_ids,
\'title_li\' => false,
\'echo\' => false,
] );
if ( ! $output ) {
return false;
} else {
return \'<ul class="page-menu ancestor-tree">\' . PHP_EOL .
$output . PHP_EOL .
\'</ul>\' . PHP_EOL;
}
}
Usage:
echo wpse_get_ancestor_tree();
Example Page Structure:
Parent Page
Child Page 01
Child Page 02
Child Page 03
Grandchild Page
Great Grandchild Page
Child Page 04
Child Page 05
Example Output (current page: Great Grandchild Page)
<ul class="page-menu ancestor-tree">
<li class="page_item page-item-1088 page_item_has_children current_page_ancestor">
<a href="http://example.com/parent-page/">Parent Page</a>
<ul class="children">
<li class="page_item page-item-1090">
<a href="http://example.com/parent-page/child-page-01/">Child Page 01</a>
</li>
<li class="page_item page-item-1092">
<a href="http://example.com/parent-page/child-page-02/">Child Page 02</a>
</li>
<li class="page_item page-item-1094 page_item_has_children current_page_ancestor">
<a href="http://example.com/parent-page/child-page-03/">Child Page 03</a>
<ul class="children">
<li class="page_item page-item-1102 page_item_has_children current_page_ancestor current_page_parent">
<a href="http://example.com/parent-page/child-page-03/grandchild-page/">Grandchild Page</a>
<ul class="children">
<li class="page_item page-item-3066 current_page_item">
<a href="http://example.com/parent-page/child-page-03/grandchild-page/great-grandchild-page/">Great Grandchild Page</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="page_item page-item-1096">
<a href="http://example.com/parent-page/child-page-04/">Child Page 04</a>
</li>
<li class="page_item page-item-1098">
<a href="http://example.com/parent-page/child-page-05/">Child Page 05</a>
</li>
</ul>
</li>
</ul>