如何显示菜单树的一部分?

发布于 2024-11-02 23:44:42 字数 1610 浏览 6 评论 0原文

我正在尝试处理 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 技术交流群。

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

发布评论

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

评论(7

孤千羽 2024-11-09 23:44:42

您可以编写一个filter_hook来完成此操作。

我的方法:使用我的自定义挂钩为 wp_nav_menu 创建一个额外的 start_in 参数:

# in functions.php add hook & hook function
add_filter("wp_nav_menu_objects",'my_wp_nav_menu_objects_start_in',10,2);

# filter_hook function to react on start_in argument
function my_wp_nav_menu_objects_start_in( $sorted_menu_items, $args ) {
    if(isset($args->start_in)) {
        $menu_item_parents = array();
        foreach( $sorted_menu_items as $key => $item ) {
            // init menu_item_parents
            if( $item->object_id == (int)$args->start_in ) $menu_item_parents[] = $item->ID;

            if( in_array($item->menu_item_parent, $menu_item_parents) ) {
                // part of sub-tree: keep!
                $menu_item_parents[] = $item->ID;
            } else {
                // not part of sub-tree: away with it!
                unset($sorted_menu_items[$key]);
            }
        }
        return $sorted_menu_items;
    } else {
        return $sorted_menu_items;
    }
}

接下来,在您的模板中,您只需使用附加 wp_nav_menu 调用>start_in 参数包含您希望子级关闭的页面的 ID:

wp_nav_menu( array( 
    'theme_location' => '<name of your menu>',
    'start_in' => $ID_of_page,
    'container' => false,
    'items_wrap' => '%3$s'
) );

You can write a filter_hook to accomplish this.

My method: create an additional start_in argument for wp_nav_menu using my custom hook:

# in functions.php add hook & hook function
add_filter("wp_nav_menu_objects",'my_wp_nav_menu_objects_start_in',10,2);

# filter_hook function to react on start_in argument
function my_wp_nav_menu_objects_start_in( $sorted_menu_items, $args ) {
    if(isset($args->start_in)) {
        $menu_item_parents = array();
        foreach( $sorted_menu_items as $key => $item ) {
            // init menu_item_parents
            if( $item->object_id == (int)$args->start_in ) $menu_item_parents[] = $item->ID;

            if( in_array($item->menu_item_parent, $menu_item_parents) ) {
                // part of sub-tree: keep!
                $menu_item_parents[] = $item->ID;
            } else {
                // not part of sub-tree: away with it!
                unset($sorted_menu_items[$key]);
            }
        }
        return $sorted_menu_items;
    } else {
        return $sorted_menu_items;
    }
}

Next, in your template you just call wp_nav_menu with the additional start_in argument containing the ID of the page you want the children off:

wp_nav_menu( array( 
    'theme_location' => '<name of your menu>',
    'start_in' => $ID_of_page,
    'container' => false,
    'items_wrap' => '%3$s'
) );
回忆追雨的时光 2024-11-09 23:44:42

我写这篇文章是为了打印您可能所在页面的子导航。如果要打印每个页面的子导航,请获取页面父级而不是获取 ID。涉及的事情会比这更多,但这只是一个开始。

$menu = wp_get_nav_menu_items( 'Primary Menu' );
$post_ID = get_the_ID();
echo "<ul id='sub-nav'>";
foreach ($menu as $item) {
    if ($post_ID == $item->object_id) { $menu_parent = $item->ID; }
    if (isset($menu_parent) && $item->menu_item_parent == $menu_parent) {
         echo "<li><a href='" . $item->url . "'>". $item->title . "</a></li>";
     }
 }
echo "</ul>";`

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.

$menu = wp_get_nav_menu_items( 'Primary Menu' );
$post_ID = get_the_ID();
echo "<ul id='sub-nav'>";
foreach ($menu as $item) {
    if ($post_ID == $item->object_id) { $menu_parent = $item->ID; }
    if (isset($menu_parent) && $item->menu_item_parent == $menu_parent) {
         echo "<li><a href='" . $item->url . "'>". $item->title . "</a></li>";
     }
 }
echo "</ul>";`
嘿看小鸭子会跑 2024-11-09 23:44:42

查看wp_list_pages()。它对于在侧边栏中提供子导航非常有用。

Check out wp_list_pages(). It is useful for providing child navigation in the sidebar.

临走之时 2024-11-09 23:44:42

mac joost的答案很好,但我想补充一点,如果你想要打印父项,那么你不应该取消设置父项,所以第18行需要相应调整:

    if($item->object_id != (int)$args->start_in) { unset($sorted_menu_items[$key]); }

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:

    if($item->object_id != (int)$args->start_in) { unset($sorted_menu_items[$key]); }
朦胧时间 2024-11-09 23:44:42

你见过 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

長街聽風 2024-11-09 23:44:42

您可以使用 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

青巷忧颜 2024-11-09 23:44:42

我已经停止探索如何在服务器端输出 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.

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