如何在<li>中添加类在 Wordpress 中使用 wp_nav_menu() 吗? [英] How to add Class in <li> using wp_nav_menu() in Wordpress?

查看:19
本文介绍了如何在<li>中添加类在 Wordpress 中使用 wp_nav_menu() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wp_nav_menu($args) 并且我想将 my_own_class CSS 类名添加到

  • 元素.>

    我想得到以下结果:

  • 怎么做?

    你不能很容易地将一个类直接添加到 LI,但是有什么理由不以稍微不同但同样具体的方式来定位它们(​​通过他们的父母 ul)?

    您可以使用 menu_class 和 menu_id 参数(附加到 LI 的 UL 父级)来设置菜单的类或 ID,然后通过 CSS 以这种方式定位 li,如下所示:

    <?php wp_nav_menu('menu_id=mymenu');?>

    对于 CSS:

    ul#mymenu li {/* 你的风格在这里 */}

    在 2013 年撰写本文时,这是添加它的最简单方法,但此后的更新添加了直接在管理导航编辑器中添加 CSS 类的功能.查看更多最新答案了解详情.

    I am using wp_nav_menu($args) and I want to add my_own_class CSS classname to the <li> element.

    I'd like to get the following result:

    <li class='my_own_class'><a href=''>Link</a>
    

    How to do that?

    解决方案

    You can't add a class directly to the LIs very easily, but is there any reason not to target them in a slightly different but equally specific way (by their parent ul)?

    You can set the class or ID of the menu with the menu_class and menu_id parameters (affixed to the UL parent of the LIs) and then target the li's via CSS that way, like so:

    <?php wp_nav_menu('menu_id=mymenu'); ?>
    

    And for CSS:

    ul#mymenu li {
        /* your styles here */
    }
    

    Edit: At the time of writing in 2013 this was the easiest way to add it, but updates since then have added the feature to add CSS classes directly in the admin navigation editor. See more recent answers for details.

    这篇关于如何在&lt;li&gt;中添加类在 Wordpress 中使用 wp_nav_menu() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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