Wordpress menu walker - help to add custom class for each submenu

I am trying to apply to my already existing menu a walker in order to add custom classes to every sub menu anchor tag. Here is my menu: wp_nav_menu(array( 'theme_location' => 'menu-top', 'container' => 'ul', 'menu_class' => 'header_nav_ul', 'menu_id' => 'header_nav_id', 'depth' => 0, )) I added the wolder and it became: wp_nav_menu(array( 'theme_location' => 'menu-top', 'container' => 'ul', 'menu_class' => 'header_nav_ul', 'menu_id' => 'header_nav_id', 'depth' => 0, 'walker' => new my_walker_nav_menu_start_el(), )) The walker function is this one: function …
Category: Web

Wordpress change wp_nav_menu walker with a custom one

I have a downloaded theme and it where the wp_nav_menu is created it looks something like this: <div class="nv-nav-wrap"> <div role="navigation" class="<?php echo esc_attr( join( ' ', $container_classes ) ); ?>" aria-label="<?php esc_attr_e( 'Primary Menu', 'neve' ); ?>"> <?php echo wp_nav_menu( [ 'theme_location' => 'primary', 'menu_id' => $menu_id, 'menu_class' => 'primary-menu-ul nav-ul' . $additional_menu_class, 'container' => 'ul', 'walker' => '\Neve\Views\Nav_Walker', 'fallback_cb' => '\Neve\Views\Nav_Walker::fallback', 'echo' => false, ] ); ?> </div> </div> I've created a child theme and I want to …
Category: Web

WordPress custom menu can't show 3. level submenu

I Use a custom walker from my menu in WordPress and my menu doesn't show all levels. https://soulofcroatia.com/ ->One Day menu item. This is my code in header.php <?php wp_nav_menu( array( 'theme_location' => 'mainmenu', 'menu' => 'mainmenu', 'container' => 'ul', 'container_class' => 'collapse navbar-collapse', 'container_id' => 'navbarCollapse', 'menu_class' => 'nav-list', 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'items_wrap' => '<ul class="%2$s">%3$s</ul>', 'depth' => 3, 'walker' => new Primary_navigation_walker(), ) ); ?> this is my code in custom-walkers.php class Primary_navigation_walker extends Walker_Nav_Menu …
Category: Web

How to display dynamic content in start_lvl function

I am creating a custom nav walker but having trouble outputting parent menu item title in start_lvl function. I want to output it right after the <div class="dropdown"> in the following start_lvl function. function start_lvl(&$output, $depth = 0, $args = array()) { $indent = str_repeat("\t", $depth); if ($depth == 0) { $out_div = ' <div class="dropdown-wrapper"><div class="dropdown">This is where I want to output parent item title'; } else { $out_div = ''; } // build html $output.= "\n" . $indent …
Category: Web

Nav Walker that shows only children and siblings of top level parent menu item

I have a nav menu that is structured like this.. Main Section 1 – Child Link 1 -- Grandchild Link 1 – Child Link 2 – Child Link 3 Main Section 2 – Child Link 1 – Child Link 2 – Child Link 3 Lets say that I am on any page within Main Section 1 (including Main Section 1 itself). I want to only show the menu items (and their children) that have Main Section 1 as their parent. …
Category: Web

How to remove a sub-menu using walker hooks/filters without relying on a custom walker?

I currently built a pretty simple tool to create a mega menu. I add a checkbox to my menu items in the Appearance > Menus section followed by a dropdown that includes template files within the theme. Here's how my current function works (or doesn't work): function mega_menu_start_el( $output, $item, $depth, $args, $id = 0 ) { $is_mega = FALSE; $in_mega = FALSE; if( $depth > 0 ) { $parent_ID = get_post_meta( $item->ID, '_menu_item_menu_item_parent', TRUE ); if( $parent_ID ) { …
Category: Web

How to get the term id inside the start lvl method of category walker?

I'm trying to implement the category walker with wp_list_categories to help me manipulate list classes, which i want to get the term id within the start_lvl method, so i tried to use $category->term_id but errors thrown. now i just want to get the term id inside the start_lvl method to match the one i used inside the start_el output, please check this line in the following class $output .= "<span id=\"$category->term_id\" class=\"toggle\" onClick=\"toggleMe(this.id);\" style=\"cursor:pointer\">&#9660</span>"; I just copied the Walker_Category class …
Category: Web

Hook or callback when wp_nav_menu() has completed

I am modifying the $output of wp_nav_menu() using a custom walker, and I need to run a function after the final closing </ul> of the entire menu. I can only see the following methods available: start_lvl() // Runs when a child menu is started end_lvl() // Runs when a child menu is completed start_el() // Runs when a menu item is started end_el() // Runs when a menu item is completed I can't find anything that runs when the top …
Category: Web

Add div to specific sub-menu

I researched in google, yandex but had no success. With this code I can set a div to sub-menu but this code add to all sub-menus, I just need to add div to specific sub-menu. class Megratron_div extends Walker_Nav_Menu { function start_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "\n$indent<div id='megatron'><ul class='sub-menu'>\n"; } function end_lvl( &$output, $depth = 0, $args = array() ) { $indent = str_repeat("\t", $depth); $output .= "$indent</ul></div>\n"; } …
Category: Web

Create Columns with Custom Walker and CSS Classes

I want to create a menu in WordPress that using the CSS Classes (from Screen Options) you can separate the menu by columns and choose when to start and end the column. I have this information.. <?php if (has_nav_menu('menu_open')) { ?> <?php $args = array( 'container' => 'ul', 'menu_class' => 'class', 'depth' => 2, 'theme_location' => 'menu_open', 'walker' => new Columns_Walker_Nav_Menu() ); wp_nav_menu($args); } ?> The original walker from WP: https://developer.wordpress.org/reference/classes/walker_nav_menu/ That I tryied to check the classes with $menu_item->classes …
Category: Web

Where to implement custom walker class?

I have created a custom walker class for changing the output of menu items. Where do I place my custom walker class file at? I have tried to ask on the Wordpress forum but it has gone 3 days with no response. I have already created the class. I just don't know where to physically put the code. Does it go in the existing class-wp-walker.php file or do I need to create my own file somewhere? Thank you much!
Category: Web

How to add class to parent a tag with a sub menu

I have been trying to add a class to the parent a tag of a sub menu using a custom walker. The current walker I am using adds the class to the parent <li>, does anybody know how to adjust this to add the class to the parent <a> tag instead. Here is the existing walker I am using: class My_Walker_Nav_Menu extends Walker_Nav_Menu{ public function display_element($el, &$children, $max_depth, $depth = 0, $args, &$output){ $id = $this->db_fields['id']; if(isset($children[$el->$id])) $el->classes[] = 'toggle-sub-nav …
Category: Web

Determine the last child of the parent menu WP Nav Walker

Is there a way to add a condition to know if the current item of the navigation walker is the last child of the menu or parent? | Item 1 | Item 2 | | - Item 1.1 | - Item 2.1 | | - Item 1.2 | - Item 2.2 | | - Item 1.3 |<----------------- Determine if current item is last child For example: if ($item->last_child(of_current_parent) { ... } So, how do you determine if the current item …
Category: Web

How to Display image from Menu Image in Walker Dropdown Select Menu

Created a custom menu list, titled Country that has flags representing countries (only Canada and USA) using the plugin Menu Image I combined this menu with Walker, to create a dropdown. I however can only access the title assigned to the images, and cannot find out where the image information is actually stored within the $item. Here is the Walker Class the I have used, this displays the Title of the selected item in the dropdown. class Walker_Nav_Menu_Dropdown extends Walker_Nav_Menu …
Category: Web

Change submenu markup

I've seen people recommending to edit the wordpress default Walker to change the menu markup and create your own. The problem with this is that I would like to change the markup of the submenu and leave the menu as it is. In the walker function I only saw the functions start_lvl and end_lvl regarding the submenu creation. The functions itself lack of very useful comments... so I understood that lvl means "level" and with that they mean "sub-menu"... But …
Category: Web

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

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 …
Category: Web

Add parent menu item in sub-menu in custom nav_walker

Currently I'm working on a project where we need to append the parent title as first item in the submenu for a mobile menu. What I try to achieve is this: <ul class="menu"> <li> <a href="linktomenu">Level 0 item</a> <ul class="submenu"> <li><a href="linktoparent">Show All Level 0 item </a></li> <li><a href="#0">Back to parent</a></li> <li><a href="linktomenu">Level 1 item</a></li> <li><a href="linktomenu">Level 1 item</a></li> </ul> </li> </ul> This is the code I currently have <?php class mobilenav_walker_nav_menu extends Walker_Nav_Menu { private $color_idx = 0; // …
Category: Web

how do you create this customize menu using wp_nav_menu function?

how do you create this customize menu using wp_nav_menu function? function.php file register_nav_menus( array( 'header-menu' => 'Header Menu', 'footer-menu' => 'Footer Menu' ) ); header.php file <?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'container' => '', ) ); ?> How do i assign class to <ul> and <li> Html Example <ul class="nav header-nav header-bottom-nav nav-center nav-uppercase"> <li class=""><a href="index.php" class="nav-top-link">Home</a></li> <li class="has-dropdown"><a href="testing.php" class="nav-top-link">Testing<i class="icon-angle-down" ></i></a> <ul class='nav-dropdown nav-dropdown-default'> <li><a href="hello.php">Hello</a></li> </ul> </li> </ul> Result Should Be Like This In Wordpress …
Category: Web

Walker class conditional based on parent menu item label or custom css class

I'm out of my shallow depth here. I'm trying to add html to the end of drop down menus whose parent item is either a specific item label or custom css class that's been added via the WP menu admin. Any help appreciated. The basics: public function end_lvl( & $output, $depth = 0, $args = null ) { parent::end_lvl( $output, $depth, $args ); if (/* top-level parent item has [specified label] or [specified css class] */){ $output .= '<div>See how …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.