【问题标题】:wordpress menu to display only if current page is descendant of a given page仅当当前页面是给定页面的后代时才显示 wordpress 菜单
【发布时间】:2013-10-01 15:31:59
【问题描述】:

我试图开发的网站有两个根页面。 作为导航结构:

  • A(0 级)
    • A1(1 级)
      • A1.1(2 级)
      • A1.2(2 级)
    • A2(1 级)
      • A2.1(2 级)
      • A2.2(2 级)
  • B(0级)
    • B1(1 级)
      • B1.1(2 级)
      • B1.2(2 级)
    • B2(1 级)
      • B2.1(2 级)
      • B2.2(2 级)

如果当前页面是 A 的后代,我需要显示 navA,同样,如果当前页面是 B 的后代,则仅显示 navB。

我尝试在 wordpress 后端创建两个单独的菜单,但只有 mainNav 一个会显示。 菜单是使用 wp_nav_menu($args) 作为导航生成的,如下所示。

echo "<div class='main_menu' data-selectname='".__('Select a page','avia_framework')."'>";

                        $avia_theme_location = 'avia';
                        $avia_menu_class = $avia_theme_location . '-menu';
                        $args = array(
                            'theme_location'    => $avia_theme_location,
                            'menu_id'           => $avia_menu_class,
                            'container_class'   => $avia_menu_class,
                            'fallback_cb'       => 'avia_fallback_menu',
                            'walker'            => new avia_responsive_mega_menu()
                        );

                        **wp_nav_menu($args);**

   echo "</div>";

有没有办法从后端实现这个功能?否则,有什么建议可以走这条路吗?

谢谢!

【问题讨论】:

    标签: php wordpress menu navigation


    【解决方案1】:
    function menu_set_parent( $sorted_menu_items, $args ) {
        $last_top = 0;
        foreach ( $sorted_menu_items as $key => $obj ) {
            // it is a top lv item?
            if ( 0 == $obj->menu_item_parent ) {
                // set the key of the parent
                $last_top = $key;
            } else {
                $sorted_menu_items[$last_top]->classes['parent'] = 'parent';
            }
        }
        return $sorted_menu_items;
    }
    add_filter( 'wp_nav_menu_objects', 'menu_set_parent', 10, 2 );
    

    试试这个它会将父类添加到 parent 。将此添加到您的functions.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-25
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多