将页面层次结构保留在wp_list_pages中,即使在子代或孙子上也是如此 [英] Keep page hierarchy in wp_list_pages, even if on a child or grandchild
问题描述
我具有以下页面结构,并且需要在具有子页面的那些页面上显示相同的结构:
I have the following structure of pages, and I need to have that same structure displayed on those pages who have child pages:
- Childpage
- - Grandchildpage
- - Other Grandchildpage
- Other Childpage
以下代码用于显示page.php上的结构:
The following code is used to display the structure on page.php:
<ul class="sidemenu-list">
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=1");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=1");
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
</ul>
在父页面上有效.但是当我在子页面或孙页面之一上时,此操作不再起作用.
This works when on the parent page. But when I'm on one of the childpages or grandchild pages, this doesn't work anymore.
按照上面的结构示例,当我进入子页面"时,只会得到以下内容:
Following above structure example, when I'm on 'Childpage' I only get the following:
- Childpage
- Other Childpage
当我进入孙子页面"时,我只会得到:
And when I'm on 'Grandchildpage' I only get:
- - Grandchildpage
- - Other Grandchildpage
即使页面是子代还是孙子,显示页面层次结构的正确方法是什么?
What is the correct way to display the page hierarchy even when the page is a child or grandchild?
推荐答案
使用以下代码:
<?php
if(!$post->post_parent){
// get the child of the top-level page
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}else{
// get the child pages if we are on the first page of the child level.
//$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
if($post->ancestors)
{
// now get an ID of the first page
// wordpress collects ID in reverse order, so the "first" will be the last page
$ancestors = end($post->ancestors);
$children = wp_list_pages("title_li=&child_of=".$ancestors."&echo=0");
}
}
if ($children) { ?>
<ul>
<?php echo $children; ?>
</ul>
<?php } ?>
这篇关于将页面层次结构保留在wp_list_pages中,即使在子代或孙子上也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!