简体   繁体   English

将当前菜单项类添加到Wordpress菜单模板

[英]Adding current menu item class to wordpress menu template

So i've been trying to use my own menu templates for using things like bootstrap within the menu items for scaling. 因此,我一直在尝试使用自己的菜单模板来在菜单项中使用自举之类的东西进行缩放。 Untill now it works incredibly well but i stumbled upon a problem. 直到现在,它的运行情况都非常好,但我偶然发现了一个问题。

By adding and deleting some classes i, in my mistake, deleted some standard classes like the .current-menu-item class for the active menu-item styling. 通过添加和删除一些类,我错了,我删除了一些标准类,例如用于活动菜单项样式的.current-menu-item class I've been trying out some of the answers on google and Stack but sadly couldn't find a good solution. 我一直在尝试Google和Stack上的一些答案,但可惜找不到合适的解决方案。 So this is what i have right now: 所以这就是我现在所拥有的:

/**
 * Class walker_home_menu
 * Menu for the home page
 */
class walker_home_menu extends Walker_Nav_Menu
{

    // Displays start of an element. E.g '<li> Item Name'
    // @see Walker::start_el()
    // In your functions.php

    function start_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';
        $class_names = $value = '';
        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $classes[] = 'menu-item-' . $item->ID;
        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';
        $output .= $indent . '';
        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
        $item_output = '<li class="col-lg-2 col-md-2 col-sm-3 col-xs-6 home-menu-item text-center">';
        $item_output .= $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $item_output .= "</li>";
        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }

    function end_el(&$output, $item, $depth = 0, $args = array())
    {
        $output .= "\n";
    }
}

The goal i would like to achieve is to add an active class for active menu-items so that i'm able to give the active menu-item a different background-color throught CSS . 我想要实现的目标是为活动菜单项添加active类,以便能够通过CSS为活动菜单项提供不同的背景色。

Thanks for the help in advance! 我在这里先向您的帮助表示感谢! Happy coding! 编码愉快!

So i found the solution to my question. 所以我找到了解决我问题的方法。 Since i think many will create, or like to create custom menu's i'm answering my own question. 由于我认为很多人会创建或喜欢创建自定义菜单,因此我在回答自己的问题。

Before posting my code i've tried to add the variable $class_items to the <li class=".."> section. 在发布代码之前,我尝试将变量$class_items添加到<li class="..">部分。 But i didn't have any succes. 但是我没有成功。 Atleast untill now. 直到现在为止。

So the answer is to add the wordpress menu item classes the correct way. 因此,答案是以正确的方式添加wordpress菜单项类。 See the code below. 请参见下面的代码。

        $item_output = '<li class="col-lg-2 col-md-2 col-sm-3 col-xs-6 home-menu-item text-center' . $class_names . '">';

The wordpress class names like .current-menu-item for .active styling will be added after the new created lay-out i've made myself. 在我创建新的布局后,将添加.active样式的.current-menu-item类的wordpress类名称。

Hope this helps everyone having this issue. 希望这对遇到此问题的所有人有所帮助。

Simple solution for those who would like to copy my code as a base, Here is the final working snippet. 对于那些想要复制我的代码作为基础的人的简单解决方案,这是最后的工作片段。

/**
 * Class walker_home_menu
 * Menu for the home page
 */
class walker_home_menu extends Walker_Nav_Menu
{

    // Displays start of an element. E.g '<li> Item Name'
    // @see Walker::start_el()
    // In your functions.php

    function start_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent\n";
    }

    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';
        $class_names = $value = '';
        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $classes[] = 'menu-item-' . $item->ID;
        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
        $class_names = $class_names ? ' ' . esc_attr($class_names) . '"' : '';
        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';
        $output .= $indent . '';
        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';
        $item_output = '<li class="col-lg-2 col-md-2 col-sm-3 col-xs-6 home-menu-item text-center' . $class_names . '">';
        $item_output .= $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $item_output .= "</li>";
        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }

    function end_el(&$output, $item, $depth = 0, $args = array())
    {
        $output .= "\n";
    }
}

Cheers and happy coding! 欢呼和快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM