删除 li 类 &菜单项和页面列表的 id

发布于 2024-10-20 20:06:09 字数 327 浏览 1 评论 0原文

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">

菜单和页面列表项带有各种自己的 li classid

如何在菜单和页面列表的 functions.php 文件中删除它们?

Example of WordPress default CSS class output:

<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.

How to remove them in functions.php file for the menu and for the pages list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不必了 2024-10-27 20:06:09

您应该能够通过连接到几个过滤器并返回空数组或字符串而不是新的类或 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() : '';
}

如果您想保留特定的类,您可以执行以下操作:

function my_css_attributes_filter($var) {
  return is_array($var) ? array_intersect($var, array('current-menu-item')) : '';
}

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:

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')) : '';
}
青柠芒果 2024-10-27 20:06:09

这是对理查德答案的补充。

如果您想将当前菜单项类别更改为其他类别。

        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;
        }

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;
        }
不打扰别人 2024-10-27 20:06:09

这是我想出的修复方法。它从 wp_nav_menu 中删除所有 id 和类,但允许您提出自己的“批准”类和/或 id 列表。它还将冗长的“当前菜单项”更改为“活动”。如果您希望保留默认的 WordPress CSS 样式,只需删除该代码部分即可。为了保持这篇文章的最少性,这里是带有代码的pastebin的链接:
http://pastebin.com/W16cxDfY - 用于您的functions.php文件
http://pastebin.com/CGx4aprf - 适用于您的模板,无论菜单在哪里

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

水染的天色ゝ 2024-10-27 20:06:09

只需 add_filter('nav_menu_item_id', '__return_false'); 作为菜单项 id

simply add_filter('nav_menu_item_id', '__return_false'); for menu item id

没有心的人 2024-10-27 20:06:09

删除 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>' );
            ?>

Best way to remove li is: Tested and Verified

           <?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>' );
            ?>
白况 2024-10-27 20:06:09

如果您只想删除所有列表类和 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; 
}

If you just want to remove all the list classes and id's, add this to 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; 
}
流星番茄 2024-10-27 20:06:09

对理查德的回答的补充:
我们需要清理留下的空类:

//Strip Empty Classes
add_filter ('wp_nav_menu','strip_empty_classes');
function strip_empty_classes($menu) {
    $menu = preg_replace('/ class=(["\'])(?!active).*?\1/','',$menu);
    return $menu;
}

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).*?\1/','',$menu);
    return $menu;
}
扛刀软妹 2024-10-27 20:06:09

我的解决方案:

$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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文