WordPress 中包含子孙的列表页面菜单

发布于 2024-10-05 12:39:02 字数 1562 浏览 5 评论 0原文

好吧,自从我制作 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”时,我想在子 ul 中显示上面菜单中的所有内容以及“子 1”(孙子 1 和 2)的所有子项在“Child 1”下,如下图所示: http://img4.imageshack.us/img4 /7868/child1.png

现在,当我访问“孙子 1”时,我想显示与“子 1”相同的菜单,仅将“孙子 1”作为当前项目,而不是像下面的草图所示:< a href="http://img819.imageshack.us/img819/1633/grandchild1.png" rel="nofollow">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 } ?>

我的计划是将菜单作为我页面上的侧边栏,正如您在草图中看到的那样。

那么有人知道如何才能达到这一结果吗?

Okay so it's been over a year since I made a WordPress theme and now I've started again but I've run into a problem with my menu on pages.

This is my current page hierarchy (I'm still only testing hence the names)

Home

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

Another page

Now when I visit "This page" I want to show a link to "This page" (current) and all the direct childs (no grandchildren here) of "This page" like in this sketch: http://img840.imageshack.us/img840/3006/thispage.png

Now when i visit a "Child 1" of "This page" I want to show everything from above menu and all childs of "Child 1" (Grandchild 1 & 2) in a sub ul under "Child 1" like in this sketch: http://img4.imageshack.us/img4/7868/child1.png

And now when I visit "Grandchild 1" I want to show the same menu as I did for "Child 1" only with "Grandchild 1" as current item instead like in this sketch: http://img819.imageshack.us/img819/1633/grandchild1.png

This is my current code based on the example in the wordpress codex (under wp_list_pages() function) for a slightly other type of menu but it isn't working very well and absolutely not well for the godchildren.

<?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 } ?>

My plan is to have the menu as a sidebar on my page as you can see in the sketches.

So does anyone know how one could achieve this result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-10-12 12:39:02

我强烈建议只使用 wp_list_pages(),然后引入 CSS 来隐藏您不希望可见的部分(该函数输出了很多类,请检查您的源代码以查看可用的类) 。

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

I would strongly recommend just using wp_list_pages(), and then bring in CSS to hide parts you don't want visible (there are plenty of classes outputted by the function, check your source to see what's available).

In addition, you're also going to make it a lot easier for search engines (and users, if they're using a limited browser or accessibility enhancement) to browse your site, since the entire hierarchy is always there in the HTML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文