列表页面菜单与wordpress中的儿童和孙子

时间:2010-12-01 06:23:09

标签: php wordpress menu parent-child grandchild

好的,所以自从我制作一个WordPress主题已经过了一年多,现在我又重新开始,但我的页面上的菜单遇到了问题。

这是我当前的页面层次结构(我仍然只测试名称)

Home

This page
 *Child 1
  **Grandchild 1
  **Grandchild 2
 *Child 2
  **Grandchild 3
  **Grandchild 4
  **Grandchild 5
 *Child 3
  **Grandchild 6

Another page

现在,当我访问“此页面”时,我想在此页面中显示“此页面”(当前)以及“此页面”的所有直接子项(此处没有孙子)的链接:http://img840.imageshack.us/img840/3006/thispage.png

现在,当我访问“此页面”的“孩子1”时,我想要显示上面菜单中的所有内容以及“孩子1”(孙1和2)的所有孩子在“孩子1”下的子ul中在此草图中:http://img4.imageshack.us/img4/7868/child1.png

现在,当我访问“Grandchild 1”时,我想显示与“Child 1”相同的菜单,只有“Grandchild 1”作为当前项目,而不是像这个草图中那样:http://img819.imageshack.us/img819/1633/grandchild1.png

这是我目前的代码,它基于wordpress codex中的示例(在wp_list_pages()函数下),用于略微其他类型的菜单,但它不能很好地工作,对于教子来说绝对不太好。

<?php
if($post->post_parent){
 $children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");
 $children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
}
else{
 $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
}  

if ($children) { ?>
 <div class="page-menu">
 <ul>
 <?php echo $children; ?>
 </ul>
 </div>
<?php } ?>

我的计划是将菜单设置为页面上的侧边栏,如草图所示。

所以有人知道如何实现这一结果吗?

1 个答案:

答案 0 :(得分:0)

我强烈建议您只使用wp_list_pages(),然后引入CSS来隐藏您不想看到的部分(函数输出了很多类,检查您的来源以查看可用的内容)。< / p>

此外,您还将使搜索引擎(以及用户,如果他们使用有限的浏览器或辅助功能增强)更轻松地浏览您的网站,因为整个层次结构始终位于HTML。