wp_nav_menu(),如何更改<li>类?
-
-
在这里您可以在ul> li> a中添加其他类 https://sdtuts.com/wordpress-add-custom-class-in-wp_nav_menu-links/Here you can add different class in ul > li >a https://sdtuts.com/wordpress-add-custom-class-in-wp_nav_menu-links/
- 0
- 2018-06-09
- Rameez SOOMRO
-
4 个回答
- 投票数
-
- 2011-12-26
使用自定义助行器,删除不需要的所有内容,然后添加课程.这是我用来获得带有清晰标记的列表的助行器: T5_Nav_Menu_Walker_Simple .
您还可以过滤
'nav_menu_css_class'
或'wp_nav_menu_items'
.但是在我看来,步行者课更容易理解和控制.Use a custom walker, remove anything you don’t need and add your classes. Here is a walker I use to get a list with clean markup: T5_Nav_Menu_Walker_Simple.
Your could also filter
'nav_menu_css_class'
or'wp_nav_menu_items'
. But a walker class is easier to understand and to control in my opinion.-
感谢Toscho,我刚刚发现在新版本的Wordpress(3.3)中,我们可以为每个菜单项添加自定义类fir,以解决我的问题.我尝试了您向我建议的脚本(T5_Nav_Menu_Walker_Simple),该脚本确实删除了
- 中的所有内容,我们如何控制要保留的元素?
Thanks Toscho, I just find that in the new version of Wordpress (3.3) we can add custom class fir each menu items wich kind of solve my problem. I tried the script you suggested to me (T5_Nav_Menu_Walker_Simple) which does strip everything from the `- `, how can we control which elements we want to keep?
- 0
- 2011-12-26
- Christian
-
@Christian您可以根据需要更改助行器,这只是一个非常基本的示例.要查看可用的信息,请向每个`li`添加一个`print_r($item,TRUE)`.然后决定如何处理.:)@Christian You can change the walker as you need, it is just a very basic example. To see which information is available, add a `print_r( $item, TRUE )` to each `li`. Then decide what to do with it. :)
- 1
- 2011-12-26
- fuxia
-
这为我指明了正确的方向,我需要的是** wp_nav_menu **,**但是**我需要更改'container_class'参数,以适合我的特定用例,在某些情况下,我会交换主菜单中的另一个菜单,但需要使类与CSS保持一致.This pointed me in the right direction, what I needed was the **wp_nav_menu**, **but** I needed to change the 'container_class' parameter, to work for my particular use case, where I on some condition swapped the main menu for another one, but needed the classes to be consistent for css.
- 0
- 2018-01-25
- D. Dan
-
-
- 2019-04-22
根据引导程序4.3的需要,将
<li>
类设置为nav-link
:function add_menu_link_class($atts, $item, $args) { $atts['class'] = 'nav-link'; return $atts; } add_filter('nav_menu_link_attributes', 'add_menu_link_class', 1, 3);
您还可以在该数组中取消设置
id
属性.Setting the
<li>
class tonav-link
, as bootstrap 4.3 needs it:function add_menu_link_class($atts, $item, $args) { $atts['class'] = 'nav-link'; return $atts; } add_filter('nav_menu_link_attributes', 'add_menu_link_class', 1, 3);
You can also unset the
id
attribute in that array. -
- 2013-06-13
作为最后提到的张贴者,您可以通过 appearance>菜单添加自己的类,并在屏幕选项中勾选CSS类.在助行器中,您可以通过以下方式访问您在此处输入的内容:
$item_output .= '<a'. $attributes .' class="'. $item->classes[0].'">';
我什至用它在菜单中添加了预先命名的图像-一点点的胡扯,但它可以工作.
<img src="theme/images/navigation/'.$item->classes[0].'" width="48" height="48">
As the last poster mentioned, you can add your own classes via appearance > menus with CSS classes ticked in the screen options. In the walker, you can access what you enter there via:
$item_output .= '<a'. $attributes .' class="'. $item->classes[0].'">';
I have even used this to add pre-named images in the menu - a little flakey, but it works.
<img src="theme/images/navigation/'.$item->classes[0].'" width="48" height="48">
我正在为我的网站构建菜单.静态看起来像这样:
我已经能够理解如何自定义
<ul>
标签,以摆脱自动的<div>
标签.但是现在,我想自定义<li>
标记,以便能够分配不同的class
名称,以通过CSS控制特定行为.当我使用wp_nav_menu()
时,输出如下:我想摆脱
<li>
标记中的<li>
,并更改class
以反映页面名称我想链接到.基本上,我想输出与这篇文章的第一段代码相同的东西.之所以这样做,是因为我使用了由纯文本CSS所控制的自定义图像.
这可能吗?我应该使用什么策略来克服这个问题?