Add Javascript to WordPress Menu

Good that it works. If it’s for a client or if you just want a cleaner code, you can do it as @Tom J Nowell suggested. Add a custom menu item, link it to nowhere or anywhere. Find out the menu item ID (every item has one), and then target that ID with jQuery. $(“#menu-item-num”).on(“click”, … Read more

How Does The Walker Class Work?

I searched and read about the walker class. I ran tests, I played around with the code and I finally understood. I hope this can be helpful to others too. You’ll need to implement the walker class for this. Here is a simple example. $defaults = array( ‘theme_location’ => ‘primary’, ‘container’ => ‘ul’, ‘menu_class’ => … Read more

How to count nav menu items?

Call wp_get_nav_menu_object to get a menu object, which will give you the count of items for that menu. Example: // Get menu object $my_menu = wp_get_nav_menu_object( ‘your-menu-name-or-slug’ ); // Echo count of items in menu echo $my_menu->count; Hope that helps.

How to Add to Each Menu Link with link text to data-attr?

Solution 1: Using customize walker I got some idea from add span class inside wp_nav_menu link anchor tag and made some changes for your requirements. 1. Add this code below to your functions.php first. class Nav_Walker_Nav_Menu extends Walker_Nav_Menu{ function start_el(&$output, $item, $depth, $args){ global $wp_query; $indent = ( $depth ) ? str_repeat( “\t”, $depth ) … Read more

How do I get the name of a menu in WordPress?

You can access the menu metadata using the wp_get_nav_menu_object function BY NAME: $menu = wp_get_nav_menu_object(“my mainmenu” ); BY SLUG: $menu = wp_get_nav_menu_object(“my-mainmenu” ); The return object as follows: Object ( term_id => 4 name => My Menu Name slug => my-menu-name term_group => 0 term_taxonomy_id => 4 taxonomy => nav_menu description => parent => 0 … Read more

Remove a menu item in menu

I think the best way to remove a menu item from a menu is by hooking to wp_nav_menu_objects filter (@Otto answer from the thread you mentioned). And the way you use is first check for the correct menu to filter, and then search for the menu item and remove it by unsetting it from the … Read more

Add Class to Specific Link in Custom Menu

you can use nav_menu_css_class filter add_filter(‘nav_menu_css_class’ , ‘my_nav_special_class’ , 10 , 2); function my_nav_special_class($classes, $item){ if(your condition){ //example: you can check value of $item to decide something… $classes[] = ‘my_class’; } return $classes; } Using this $item you can any condition you want. and this will add the class to the specific li and you … Read more

Error “Trying to get property of non-object” with Custom Walker for wp_nav_menu

I get this error when there are no menus defined or no menus set for the location at Appearance->Menus. When that occurs wp_nav_menu uses a page walker fallback. The fallback (default) for wp_nav_menu is wp_walker_page which uses wp_page_menu which uses wp_list_pages which uses walk_page_tree which uses Walker_Page not Walker_Nav_Menu. And apparently the two walkers are … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)