向 WordPress 主题添加子菜单
我想将 WordPress 菜单的子菜单添加到我的主题中。我想使用Wordpress 3.0的wp_nav_menu功能。换句话说,我想查看子菜单而不是子页面,这意味着 wp_list_pages 不是正确的函数,因为我想要子菜单而不是子页面。
我们假设菜单结构如下所示:
- Home
- Entry1
- 条目3
- 条目4
- 条目2
- 条目5
- 条目6
我希望如果有人单击 Entry1(并将其设为父项),则主题仅显示该条目的子菜单。对于 Entry1 来说,它是:
- Entry3
- Entry4
我知道有这样的代码:
<?php
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if($children) { echo('<ul>'.$children.'</ul>'); }
?>
但是,重点是我正在谈论菜单结构,而不是页面结构。哦,深度参数不起作用,因为它在这里表示到,而不是来自。
我认为可能有一个带有定制步行器的解决方案,但我不知道如何实现。
wp_nav_menu 函数参考 http://codex.wordpress.org/Template_Tags/wp_nav_menu
我正在寻找解决方案这个问题困扰了这么久所以请帮助我。多谢。
I want to add a submenu of a wordpress menu into my theme. I want to use the wp_nav_menu function of Wordpress 3.0. And in other words, I want to see the submenu not the subpages which means that wp_list_pages is not the right function because I want the submenu and not the subpages.
Let's assume the menu structure looks like that:
- Home
- Entry1
- Entry3
- Entry4
- Entry2
- Entry5
- Entry6
I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. In the case of Entry1 it's:
- Entry3
- Entry4
I know that there is a code like that:
<?php
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if($children) { echo('<ul>'.$children.'</ul>'); }
?>
However, the point is that I'm talking about the menu structure and not the page structure. Oh, and the depth parameter does not work because it means to here and not from here.
I think there could be a solution with a custom walker but I don't know how to implement that.
Function reference for wp_nav_menu
http://codex.wordpress.org/Template_Tags/wp_nav_menu
I'm looking for a solution for this problem for so long so please help me. Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了使其正常工作,我必须在页面加载后立即隐藏 .sub-menu。然后,通过定位“.current_page_item .sub-menu”仅显示相关子菜单
In order to get this to work I had to hide the .sub-menu as soon as the page loaded. Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu"
这应该有帮助:来自 http://www.svennerberg。 com/2009/02/creating-a-submenu-in-wordpress/
This should help: From http://www.svennerberg.com/2009/02/creating-a-submenu-in-wordpress/
一种解决方案是在页面上放置另一个 wp_nav_menu 函数并修改 css 以隐藏不活动的菜单项。
One solution is to put another wp_nav_menu function on page and to modify css to hide inactive menu items.