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

menus - How to add a submenu toggle button inside all "li" elements that have a submenu?

programmeradmin2浏览0评论

I'm building a mobile navigation menu. I wan't the menu to work like this:

In this image you see a mobile nav with multiple depth levels.

This is how I wan't the code structure to look like:

<ul>
    <li></li>
    <li class="menu-item-has-children">
        <div class="toggle-button"></div>
        <a href=""></a>
        <ul class="submenu">
            <li></li>
            <li class="menu-item-has-children">
                <div class="toggle-button"></div>
                <a href=""></a>
                <ul class="submenu"></ul>
            </li>
        </ul>
    </li>
</ul>

You can see a menu item "Products" with children that are hidden.

Below you see "Industries". Here the children are shown when the toggle button to the right is clicked. The children of the next submenu level can also be toggled and so on.

I'm outputting all menu items with 'depth' => '0', and now I wan't submenus to be expandable.

To do this I need to add a toggle HTML element (see the code structure above) for which I can use jQuery to toggle an "active" class on the parent element, which then reveals the children.

My question

How can I add this toggle element inside all <li> elements, that have children, for all depth levels?

I think I need to filter wp_nav_menu or add a custom walker function, but I'm not sure how.

I'm building a mobile navigation menu. I wan't the menu to work like this:

In this image you see a mobile nav with multiple depth levels.

This is how I wan't the code structure to look like:

<ul>
    <li></li>
    <li class="menu-item-has-children">
        <div class="toggle-button"></div>
        <a href=""></a>
        <ul class="submenu">
            <li></li>
            <li class="menu-item-has-children">
                <div class="toggle-button"></div>
                <a href=""></a>
                <ul class="submenu"></ul>
            </li>
        </ul>
    </li>
</ul>

You can see a menu item "Products" with children that are hidden.

Below you see "Industries". Here the children are shown when the toggle button to the right is clicked. The children of the next submenu level can also be toggled and so on.

I'm outputting all menu items with 'depth' => '0', and now I wan't submenus to be expandable.

To do this I need to add a toggle HTML element (see the code structure above) for which I can use jQuery to toggle an "active" class on the parent element, which then reveals the children.

My question

How can I add this toggle element inside all <li> elements, that have children, for all depth levels?

I think I need to filter wp_nav_menu or add a custom walker function, but I'm not sure how.

Share Improve this question edited May 7, 2019 at 2:23 LoicTheAztec 3,39117 silver badges24 bronze badges asked Oct 13, 2017 at 12:50 SkovsgaardSkovsgaard 2371 gold badge2 silver badges9 bronze badges 2
  • A custom walker is what you need. Try some of the tutorials around the web verbatim, then work on tweaking it slowly toward what you need. Then if you have a more specific question or portion where you're stuck, post it here and you may find more assistance. :) – WebElaine Commented Oct 13, 2017 at 18:47
  • Thanks for your reply. After tinkering a bit i managed to come up with a solution. See my answer below. – Skovsgaard Commented Oct 16, 2017 at 12:49
Add a comment  | 

1 Answer 1

Reset to default 2

I managed to solve this:

Create the function

So I created a new custom walker function in functions.php and copied all the code of the core wp nav walker function (wp-includes/class-walker-nav-menu.php).

Check for a menu item that has children

I then added this array_search in public function start_el to check if the parent element has the class "menu-item-has-children":

$has_children = array_search ( 'menu-item-has-children' , $classes );

Output the toggle button

I then output the collapse element at the end of the start_el function if $has_children is true:

if($has_children != false) :
    $item_output .= '<div class="toggle-button"></div>';
endif;

Making it all work

I then added jQuery to toggle a class on the parent element of the toggle button.

jQuery(".toggle-button").click( function (){
    $(this).parent().toggleClass("menu-collapsed");
});

This works perfectly, but is there a better way to check if the menu item has children? Searching for a class name just doesn't feel like the best solution.

发布评论

评论列表(0)

  1. 暂无评论