wordpress 删除菜单项和页面列表的 li 类和 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5222140/
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
remove li class & id for menu items and pages list
提问by Cam
Example of WordPress default CSS class output:
WordPress 默认 CSS 类输出示例:
<li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-55">
<li class="page_item page-item-37">
The menu and pages list item come with various own li class
and id
.
菜单和页面列表项带有各种自己的 liclass
和id
。
How to remove them in functions.php
file for the menu and for the pages list?
如何functions.php
在菜单和页面列表的文件中删除它们?
回答by Richard M
You should be able to remove them by hooking into a couple of filters and returning empty arrays or strings rather than new classes or ids:
您应该能够通过挂钩几个过滤器并返回空数组或字符串而不是新类或 id 来删除它们:
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
return is_array($var) ? array() : '';
}
If you wanted to keep particular classes you could do something like this:
如果你想保留特定的类,你可以这样做:
function my_css_attributes_filter($var) {
return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}
回答by alhoseany
this is an addition on top of Richard answer.
这是对理查德回答的补充。
in case you want to change the current-menu-item class to something else.
如果您想将当前菜单项类更改为其他内容。
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
if(is_array($var)){
$varci= array_intersect($var, array('current-menu-item'));
$cmeni = array('current-menu-item');
$selava = array('selectedmenu');
$selavaend = array();
$selavaend = str_replace($cmeni, $selava, $varci);
}
else{
$selavaend= '';
}
return $selavaend;
}
回答by Atif Tariq
Best way to remove li is: Tested and Verified
删除 li 的最佳方法是:经过测试和验证
<?php
$menuParameters = array(
'theme_location' => 'header-menu-top',
'container' => false,
'echo' => false,
'items_wrap' => '%3$s',
'depth' => 0,
);
echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );
?>
回答by RevelationTravis
Here is a fix that I've come up with. It removes all the id's and classes from the wp_nav_menu, but allows you to come up with your own "approved" list of classes and/or id's. It also changes the lengthy "current-menu-item" to "active". If you prefer to keep the the default WordPress CSS styles, just delete that section of the code. For the sake of keeping this post minimal, here are the links to the pastebin with the code:
http://pastebin.com/W16cxDfY- for your functions.php file
http://pastebin.com/CGx4aprf- for your template, wherever the menu goes
这是我想出的修复方法。它从 wp_nav_menu 中删除所有 id 和类,但允许您提出自己的“批准”类和/或 id 列表。它还将冗长的“当前菜单项”更改为“活动”。如果您希望保留默认的 WordPress CSS 样式,只需删除该部分代码即可。为了保持这篇文章的最小化,这里是带有代码的 pastebin 的链接:
http: //pastebin.com/W16cxDfY- 对于您的 functions.php 文件
http://pastebin.com/CGx4aprf- 对于您的模板,无论菜单在哪里
回答by vralle
simply add_filter('nav_menu_item_id', '__return_false');
for menu item id
仅add_filter('nav_menu_item_id', '__return_false');
用于菜单项 ID
回答by Randy Kilwag
If you just want to remove all the list classes and id's, add this to functions.php
如果您只想删除所有列表类和 ID,请将其添加到 functions.php
add_filter('nav_menu_item_id', 'filter_menu_id');
add_filter( 'nav_menu_css_class', 'filter_menu_li' );
function filter_menu_li(){
return array('');
}
function filter_menu_id(){
return;
}
回答by Olivier C
My solution:
我的解决方案:
$defaults = array(
'theme_location' => '',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => '',
'menu_id' => '',
'echo' => false, // param important
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '',
'depth' => -1,
'walker' => ''
);
ob_start();
echo preg_replace( '#<li[^>]+>#', '<li>', wp_nav_menu( $defaults ) );
$mainNav = ob_get_clean();
// In the page :
echo $mainNav;
回答by Zeno Popovici
An addition to Richard's answer: We need to clean up the empty classes left behind:
理查德回答的补充:我们需要清理留下的空类:
//Strip Empty Classes
add_filter ('wp_nav_menu','strip_empty_classes');
function strip_empty_classes($menu) {
$menu = preg_replace('/ class=(["\'])(?!active).*?/','',$menu);
return $menu;
}