根据当前页面,我列出了属于父祖先的所有页面的链接。然而,当我访问孙子时,链接层次结构中的顶部父页面将消失。这就是我所尝试的:
<?php
// display the sub pages from the current page item
if($post->post_parent) {
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
$titlenamer = get_the_title($post->post_parent);
$permalink = get_permalink($post->post_parent);
} else {
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
$titlenamer = get_the_title($post->ID);
$permalink = get_permalink($post->ID);
}
if ($children) {
?>
<h2><a href="<?php echo $permalink; ?>"><?php echo $titlenamer; ?></a></h2>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
我想要的是根据当前查看的页面按层次列出所有带有子页面的页面。
最合适的回答,由SO网友:Grávuj Miklós Henrich 整理而成
<?php
$parent = array_reverse(get_post_ancestors($post->ID));
$titlenamer = get_the_title($parent[0]);
$permalink = get_permalink($parent[0]);
if ($post->post_parent) {
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
$children = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");
if ($children) { ?>
<h2><a href="<?php echo $permalink; ?>"><?php echo $titlenamer; ?></a></h2>
<ul id="subnav">
<?php echo $children; ?>
</ul>
<?php
}
?>
This will work :)