CSS wp_nav_menu - 在 UL 上添加类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22742630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 02:10:42  来源:igfitidea点击:

wp_nav_menu - add class on UL

csswordpresstwitter-bootstrap

提问by JunM

I am learning wordpresstogether with bootstrapand somehow I can't add classon ULtag.

我正在学习wordpress用在一起bootstrap,不管怎样我不能添加classUL标签。

In the screenshot, I want to add class nav nav-tabson ULbut it was added on parent div

在屏幕截图中,我想添加类nav nav-tabsUL但它已添加到父级div

$defaults = array(
    'menu_class'=> 'nav nav-tabs',        
);

wp_nav_menu( $defaults ); 

Inspected element: enter image description here

检查元素: 在此处输入图片说明

Referrence: http://codex.wordpress.org/Function_Reference/wp_nav_menu

参考:http://codex.wordpress.org/Function_Reference/wp_nav_menu

回答by andreivictor

First of all, you need to create a custom navigation menu from Appearance -> Menus.

首先,您需要从Appearance -> Menus.

Then, use the wp_nav_menuwith the following parameters:

然后,使用wp_nav_menu具有以下参数的 :

<?php 
$args = array(
    'menu_class' => 'nav nav-tabs',        
    'menu' => '(your_menu_id)'
);
wp_nav_menu( $args ); 
?>

There's a lot you can read about Wordpress Menus. I suggest the following:
http://codex.wordpress.org/Navigation_Menus
http://www.paulund.co.uk/how-to-register-menus-in-wordpress

关于 Wordpress 菜单,您可以阅读很多内容。我建议如下:
http: //codex.wordpress.org/Navigation_Menus
http://www.paulund.co.uk/how-to-register-menus-in-wordpress

回答by Brane

You need to specify the container element, in our case 'ul'tag, and than specify the class that we will assign in 'menu_class'. Here is the sample code:

您需要在我们的 case'ul'标记中指定容器元素,然后指定我们将在 中分配的类'menu_class'。这是示例代码:

wp_nav_menu( array(
    'theme_location' => 'top-menu',
    'container' => 'ul',
    'menu_class'=> '[add-your-class-here]'
 ) );

回答by Albertino Carvalho

Update: this was caused by the fact that i didn't have a menu created in the menu page.

更新:这是因为我没有在菜单页面中创建菜单。

I want to add a class to the ulof wp_nav_menu. I tried this code:

我想在ulof 中添加一个类wp_nav_menu。我试过这个代码:

<?php

$defaults = array(
    'menu_class'      => 'menu',
    'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
);

wp_nav_menu( $defaults );

?>

According to wordpress codex changing the menu from 'menu_class' => 'menu', should change the class of the ul, but instead it changes the class of the divwrapping the ul.

根据wordpress codex改变菜单'menu_class' => 'menu',应该改变的类ul,而是改变div包装的类ul

回答by Santiago Alejandro

I had the same problem, so I changed the word "theme_location" to "name" and it works perfectly.

我遇到了同样的问题,所以我把“theme_location”这个词改成了“name”,它运行得很好。

Example:

例子:

$defaults = array(
    '[INSTEAD OF PUTTING "theme_location" PUT "name"]'  => 'THE NAME OF YOUR "theme_location"',
    'menu_class' => 'YOUR CLASS', **[It will change the <UL> class]**
    );

    wp_nav_menu( $defaults );

So for your code:

所以对于你的代码:

wp_nav_menu( array(
    'name' => 'top-menu',
    'menu_class'=> 'YOUR CLASS'
 ) );

 wp_nav_menu( $defaults );

enter image description here

在此处输入图片说明

You can also put it into a container like a <DIV> or a <NAV> etc.

您也可以将其放入容器中,例如 <DIV> 或 <NAV> 等。

Example:

例子:

$defaults = array(
    'name'  => 'top-menu',
    'menu_id'         => 'YOUR ID',
    'container'       => 'nav',
    'container_class' => 'YOUR CONTAINER CLASS (<nav> in this case)',
    'menu_class'      => 'YOUR CLASS FOR <UL>',
    );

    wp_nav_menu( $defaults );

enter image description here

在此处输入图片说明

回答by Umair Razzaq

This is how you should do it, it works for me.

这就是你应该做的,它对我有用。

<?php wp_nav_menu( $menu_meta );

$menu_meta = array(
    'menu' => 'MENU-NAME-HERE',
    'items_wrap' => '<ul id="MENU-ID" class="MENU-CLASS">%3$s</ul>'
); ?>