我不完全理解你的问题是什么,主要是因为你在谈论父母和孩子,但这永远不会显示页面的家长,更不用说祖父母了。
我在这里对您的代码进行了注释,以向您展示代码中到底发生了什么。
function wpb_list_child_pages_popup() {
// Get the current WP Post object
global $post;
/**
* If post_type of current WP Post object is \'page\'
* and the post_parent in the WP Post object is set
*/
if ( is_page() && $post->post_parent )
/**
* Get all wordpress pages that are a child of this pages parent.
* In other words get THIS page and all it\'s siblings
*/
$childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->post_parent . \'&echo=0\' );
/**
* If this page doesn\'t have a parent
*/
else
/**
* Get all wordpress pages that are a child of this page
*/
$childpages = wp_list_pages( \'sort_column=menu_order&title_li=&child_of=\' . $post->ID . \'&echo=0\' );
if ( $childpages ) {
$string = \'<ul id="child-menu">\' . $childpages . \'</ul>\';
}
return $string;
}
add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');
我猜你只是想要一个包含所有页面的列表,这些页面通过(祖辈)父母、(祖辈)子女或兄弟姐妹“连接”。此函数返回一个包含所有这些内容的字符串,无论您在哪个同级上。
function wpb_list_child_pages_popup() {
// Get the current WP Post object
global $post;
if ( is_page() ) {
/**
* Get \'first\' parent, the highest ranking post parent which is the ancestor of all.
* @var array
*/
$ancestor = array_reverse( get_post_ancestors( $post->ID ) );
// If there is no ancestor, set ancestor to this page.
if ( empty( $ancestor ) ) {
$ancestor_id = $post->ID;
}
else {
$ancestor_id = $ancestor[0];
}
// Get childpages from ancestor
$pages = wp_list_pages( array(
\'child_of\' => $ancestor_id,
\'title_li\' => \'\',
\'echo\' => 0
) );
$string = \'<ul id="child-menu">\';
// Ancestor is not included in the $pages so add this manually
$string .= \'<li class="ancestor"><a href="\' . get_the_permalink( $ancestor_id ) . \'">\' . get_the_title( $ancestor_id ) . \'</a></li>\';
$string .= \'<ul class="children">\';
$string .= $pages;
$string .= \'</ul>\';
$string .= \'</ul>\';
return $string;
}
}
add_shortcode(\'wpb_childpages_popup\', \'wpb_list_child_pages_popup\');