duanmei2459 2019-04-06 10:11
浏览 31

将类添加到Wordpress菜单

I add classes to each Wordpress Menu Item but they are not appearing in the code.

My menu:

<?php $wp_custom_nav = array(
        'theme_location'  => 'primary',
    'container'       => 'nav',
        'container_class' => 'nav',
    'echo'            => false,
    'fallback_cb'     => false,
    'items_wrap'      => '%3$s',
    'depth'           => 0
    );
    echo strip_tags(wp_nav_menu( $wp_custom_nav ), '<nav><a>');
    ?>

My function.php include

    register_nav_menus( array(
        'primary' => esc_html__( 'Primary', 'yewtree' ),
    ) );

So I have

<nav>
  <a></a>
  <a></a>
  ...
</nav>

but the doesn't have any class that I added at Wordpress administration menu. Why is that?

  • 写回答

1条回答 默认 最新

  • dthtvk3666 2019-04-07 12:05
    关注

    wp_nav_menu uses Walker_Nav_Menu by default. And this is the part responsible for printing the classes:

        /**
         * Filters the CSS classes applied to a menu item's list item element.
         *
         * @since 3.0.0
         * @since 4.1.0 The `$depth` parameter was added.
         *
         * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
         * @param WP_Post  $item    The current menu item.
         * @param stdClass $args    An object of wp_nav_menu() arguments.
         * @param int      $depth   Depth of menu item. Used for padding.
         */
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
        $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    
        /**
         * Filters the ID applied to a menu item's list item element.
         *
         * @since 3.0.1
         * @since 4.1.0 The `$depth` parameter was added.
         *
         * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
         * @param WP_Post  $item    The current menu item.
         * @param stdClass $args    An object of wp_nav_menu() arguments.
         * @param int      $depth   Depth of menu item. Used for padding.
         */
        $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
        $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    
        $output .= $indent . '<li' . $id . $class_names . '>';
    

    So, as you can see, the classes are applied to <li> tags and not to <a> tags.

    So they are applied, but then... You do this:

    echo strip_tags(wp_nav_menu( $wp_custom_nav ), '<nav><a>');
    

    So you strip all tags other than <nav> and <a> - so the <li> tags are stripped (and the classes are gone too).

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示