【问题标题】:Wordpress: Remove Classes from Custom added LinksWordpress:从自定义添加的链接中删除类
【发布时间】:2013-01-03 06:01:57
【问题描述】:

我想知道是否有任何方法可以仅从自定义链接中删除类?至少,我想从我的自定义链接“current-menu-ancestor current-menu-parent”中删除这些类,而不是从动态链接中删除。

我正在使用这段代码:

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_intersect($var, array('current-menu-ancestor current-menu-parent')) : '';
}

它正在从所有静态和动态链接中删除 css 类。我只想从自定义链接中删除 css 类。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您可以检查链接是否包含menu-item-object-custom 类,并使用结果选择性地过滤 CSS 类:

    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($classes) {
        // if this is not a custom link and not the home page, return the $classes intact, otherwise filter $classes 
        return is_array($classes) ? 
            (in_array("menu-item-object-custom", $classes) || is_front_page())? 
                array_diff($classes, array('current-menu-ancestor current-menu-parent'))
                : $classes // not a custom link
            : ''; // not an array
    }
    

    【讨论】:

    • Mate 非常感谢您的帮助,但不幸的是,我的课程修改也干扰了我的其他网站页面,现在简单地说.. 抱歉让您感到困惑,请参阅:avanzasolutions.com 在此网站顶部链接有突出显示的链接“产品和服务”基本上不是那个页面,您可以单击它并查看它自己的页面,但我不知道主页上为什么它突出显示。 :s
    • 您是否希望从该菜单项中删除 current-menu-ancestorcurrent-menu-parent 类,因为它是主页?如果是这样,您可以在删除类时检查 is_home(),或者检查该特定菜单 ID 的类 - 对于“产品和服务”,它是 menu-item-248
    • 是的,想从主页中删除
    • 可以用 if is_front_page() 再次输入代码,你的整个代码。
    • is_home()is_front_page() 应该都可以工作,上面更新了代码。
    猜你喜欢
    • 2012-04-10
    • 1970-01-01
    • 2017-07-29
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多