wp_nav_menu 更改子菜单类名称? [英] wp_nav_menu change sub-menu class name?

查看:30
本文介绍了wp_nav_menu 更改子菜单类名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 WordPress 本身生成的子 <ul class="sub-menu"> 更改为自定义类名?

Is there a way to change the child <ul class="sub-menu"> generated by WordPress itself to a custom class name?

我知道父

    您可以使用 'menu_class' => 删除或更改名称.'新名称'.

    I know the parent <ul> you can remove or change the name with 'menu_class' => 'newname'.

    我找不到答案.Itried 'submenu_class' =>'自定义名称'.这对我来说似乎是合乎逻辑的,但显然这不是正确的.

    I couldn't find the answer. Itried 'submenu_class' => 'customname'. It seems logic to me, but obviously that is no the right one.

    有什么想法吗?

    推荐答案

    对此没有选项,但您可以扩展 WordPress 用于创建菜单 HTML 的walker"对象.只需要覆盖一种方法:

    There is no option for this, but you can extend the 'walker' object that WordPress uses to create the menu HTML. Only one method needs to be overridden:

    class My_Walker_Nav_Menu extends Walker_Nav_Menu {
      function start_lvl(&$output, $depth) {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<ul class=\"my-sub-menu\">\n";
      }
    }
    

    然后,您只需将 walker 的实例作为参数传递给 wp_nav_menu,如下所示:

    Then you just pass an instance of your walker as an argument to wp_nav_menu like so:

    'walker' => new My_Walker_Nav_Menu()
    

    这篇关于wp_nav_menu 更改子菜单类名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆