php Wordpress 添加一个类到菜单链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20752318/
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
Wordpress add a class to menu link
提问by Raunak Kathuria
I need your help. I need to add a specific class on the link that wordpress generates in the menu. What should I edit? The HTML output in my case is:
我需要你的帮助。我需要在 wordpress 在菜单中生成的链接上添加一个特定的类。我应该编辑什么?我的情况下的 HTML 输出是:
<nav class="nav" role="navigation">
<ul>
<li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="example.com">Sample Page</a></li>
<li id="menu-item-9" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-9"><a href="http://example.com">Prova sample</a></li>
</ul></nav
the code in the header file that generate the menu is:
生成菜单的头文件中的代码是:
<!-- nav -->
<nav class="nav" role="navigation">
<?php html5blank_nav(); ?>
</nav>
<!-- /nav -->
回答by techmichelle
Adds a class https://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes
添加类 https://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes
function add_specific_menu_location_atts( $atts, $item, $args ) {
// check if the item is in the primary menu
if( $args->theme_location == 'primary' ) {
// add the desired attributes:
$atts['class'] = 'menu-link-class';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );
回答by Raunak Kathuria
You can do it through admin panel also
您也可以通过管理面板完成
In Appearance > Menus, click the Screen Options tab
Under Show advanced menu properties, check CSS Classes
Now expand any menu item to reveal the CSS Classes (optional) text
input.Enter your class name and save your menu to apply the class to the
menu item
在外观 > 菜单中,单击屏幕选项选项卡
在显示高级菜单属性下,选中 CSS 类
现在展开任何菜单项以显示 CSS 类(可选)文本
输入。输入您的班级名称并保存您的菜单以将班级应用于
菜单项
http://sevenspark.com/how-to/how-to-add-a-custom-class-to-a-wordpress-menu-item
http://sevenspark.com/how-to/how-to-add-a-custom-class-to-a-wordpress-menu-item
回答by Nick Kuijpers
After creating some WordPress sites based on the Bootstrap framework, i've come up with a solution of easily manipulating the WordPress navigation output by initializing the classes you want. This will make sure your enduser will not be needed to add all the custom classes when the client add's a new menu item.
在基于 Bootstrap 框架创建了一些 WordPress 站点之后,我想出了一个通过初始化你想要的类来轻松操纵 WordPress 导航输出的解决方案。这将确保您的最终用户在客户端添加新菜单项时不需要添加所有自定义类。
Maybe it could help someone;
也许它可以帮助某人;
https://github.com/nickkuijpers/WordPress-Extended-Bootstrap-Nav-Walker
https://github.com/nickkuijpers/WordPress-Extended-Bootstrap-Nav-Walker
回答by Nick Kuijpers
Try edit html5blank_nav() function, in functions.php file
尝试在functions.php文件中编辑html5blank_nav()函数
// HTML5 Blank navigation
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'header-menu',
'menu' => '',
'container' => 'div',
'container_class' => 'menu-{menu slug}-container',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}

