最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Remove all nav menu classes ( but keep useful ones... )

programmeradmin2浏览0评论

I'm trying to remove all menu-item classes (except for .current-menu-{item/parent/ancestor} and .menu-item-has-children)

function custom_nav_menu_css_class($classes) {
    $classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
    return $classes;
}
add_filter('nav_menu_css_class', 'custom_nav_menu_css_class');

This almost does the job, except it removes .menu-item-has-children? Any idea what I should change, to exclude it from being removed?

(P.S. I'd rather not use a custom walker...)

I'm trying to remove all menu-item classes (except for .current-menu-{item/parent/ancestor} and .menu-item-has-children)

function custom_nav_menu_css_class($classes) {
    $classes = preg_replace('/^((menu|page)[-_\w+]+)+/', '', $classes);
    return $classes;
}
add_filter('nav_menu_css_class', 'custom_nav_menu_css_class');

This almost does the job, except it removes .menu-item-has-children? Any idea what I should change, to exclude it from being removed?

(P.S. I'd rather not use a custom walker...)

Share Improve this question edited Feb 8, 2014 at 20:32 Lisa asked Feb 8, 2014 at 20:26 LisaLisa 1358 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You could work with a white-list and replace the regular expression with something more readable:

add_filter( 'nav_menu_css_class', function( $classes ) {

    $allowed = [
        'menu-item-has-children',
        'current-menu-item'
    ];

    return array_intersect( $classes, $allowed );
});

That would make it easier to maintain the white-list too.

发布评论

评论列表(0)

  1. 暂无评论