如何显示菜单树的一部分?
我正在尝试处理 WordPress 3.0。这是一件很酷的事情,但我无法解决一个问题。例如,我有这样的菜单树。菜单树是由页面构建的。
Home
news
video
audio
Blog
About author
Favourite colors
red
blue
green
My car
wheels
tires
这个想法是: 主菜单由根元素组成:主页、博客、我的车 在左侧,我想显示当前活动根元素的子元素。
例如,如果用户位于“主页”页面,则在左侧他应该看到:
news
video
audio
如果用户位于“博客”页面,他应该看到:
About author
Favourite colors
red
blue
green
我找不到可以执行此操作的 API。你能建议我吗,我在哪里可以找到它?
UPD: @杰森麦克里 我已经看到 wp_list_pages() 并尝试过。我不明白如何使用它: 请查看我的页面模板:
<?php
/*
Template Name: page_news
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<h1>page_news</h1>
<h1>Children menu:</h1>
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
<div id="container">
<div id="content" role="main">
<?php
/** Get category id by name*/
//$catId = get_category_by_slug('news')->term_id;
query_posts('category_name=news');
get_template_part( 'loop', 'page' );
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
请参阅这行代码:
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
我确实有 id=8 的页面(我在 URL 中看到它)。 id=8 的页面有多个子页面。 我想打印它们,但它们没有被打印。函数wp_list_pages()的输出什么也没有。 我不知道为什么...:(
I'm trying to deal with Wordpress 3.0. It's rather cool thing but I can't get on with one problem. For example, I have such menu tree. Menu tree is constructed from pages.
Home
news
video
audio
Blog
About author
Favourite colors
red
blue
green
My car
wheels
tires
The Idea is:
main menu consists of root elements: home, blog, my car
On the left side I would like to display children elements of current active root element.
For exammple if person is on the "home" page, on the left part he should see:
news
video
audio
If user is on the "Blog" page, he should see:
About author
Favourite colors
red
blue
green
I can't find an API to do that. Can you sugest me please where can I find it?
UPD:
@Jason McCreary
I've seen I've seen wp_list_pages() and tried it. I din't get how can I use it:
Please, see my template for a page:
<?php
/*
Template Name: page_news
* @package WordPress
* @subpackage Twenty_Ten
* @since Twenty Ten 1.0
*/
get_header(); ?>
<h1>page_news</h1>
<h1>Children menu:</h1>
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
<div id="container">
<div id="content" role="main">
<?php
/** Get category id by name*/
//$catId = get_category_by_slug('news')->term_id;
query_posts('category_name=news');
get_template_part( 'loop', 'page' );
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
See this line of code:
<?php wp_list_pages('echo=0&child_of=8&title_li='); ?>
I do have the page with id=8 (I see it in URL). Page with id=8 has several children.
I want to print them, but they are not printed. The output of the function wp_list_pages() is nothing.
I don't know why... :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以编写一个filter_hook来完成此操作。
我的方法:使用我的自定义挂钩为
wp_nav_menu
创建一个额外的start_in
参数:接下来,在您的模板中,您只需使用附加
wp_nav_menu
调用>start_in 参数包含您希望子级关闭的页面的 ID:You can write a filter_hook to accomplish this.
My method: create an additional
start_in
argument forwp_nav_menu
using my custom hook:Next, in your template you just call
wp_nav_menu
with the additionalstart_in
argument containing the ID of the page you want the children off:我写这篇文章是为了打印您可能所在页面的子导航。如果要打印每个页面的子导航,请获取页面父级而不是获取 ID。涉及的事情会比这更多,但这只是一个开始。
I wrote this to print sub-navs of the pages you may be on. If you want to print out the sub-navigation for each of the pages, get the page parent instead of getting the ID. There would be more involved than that, but it's a start.
查看wp_list_pages()。它对于在侧边栏中提供子导航非常有用。
Check out wp_list_pages(). It is useful for providing child navigation in the sidebar.
mac joost的答案很好,但我想补充一点,如果你想要打印父项,那么你不应该取消设置父项,所以第18行需要相应调整:
mac joost's answer is great, but I would add that if you want the parent item to print, then you shouldn't unset the parent, so line 18 needs to be adjusted accordingly:
你见过 wp_list_pages 吗?
http://codex.wordpress.org/Function_Reference/wp_list_pages
仔细查看 child_of 属性
have you seen wp_list_pages?
http://codex.wordpress.org/Function_Reference/wp_list_pages
look closer on child_of attribute
您可以使用 Breadcrumb navxt 插件。它完全符合您的需求,而且非常棒。
面包屑 NavXT Pugin
You can use Breadcrumb navxt plugin. It does exactly what you are looking for and its really great.
Breadcrumb NavXT Pugin
我已经停止探索如何在服务器端输出 worpress 站点分类的自定义部分。我只是使用 jquery 从主菜单复制活动分类分支并将其粘贴到我需要的页面容器中。
I've stopped to explore how to output custom part of worpress site taxonomy on the server-side. I just use jquery to copy active taxonomy branch from main menu and paste it to the page container I need.