wp nav menu: add attributes to menu items [duplicate]

You can use a Custom Walker to add attributes to the menu items. Basically, you add a parameter ‘walker’ to the wp_nav_menu() options and call an instance of an enhanced class: wp_nav_menu( array ( ‘menu’ => ‘main-menu’, ‘container’ => FALSE, ‘container_id’ => FALSE, ‘menu_class’ => ”, ‘menu_id’ => FALSE, ‘depth’ => 1, ‘walker’ => new … Read more

Limit top level menu items on wp_nav_menu

A few lines before that filter is this line: $sorted_menu_items = apply_filters( ‘wp_nav_menu_objects’, $sorted_menu_items, $args ); That filter passes the top level menu item objects before walk_nav_menu_tree() is called. Here is some test code: add_filter( ‘wp_nav_menu_objects’, ‘my_nav_menu_objects’, 10, 2 ); function my_nav_menu_objects( $sorted_menu_items, $args ) { wp_die( ‘<pre>’ . var_export( $sorted_menu_items, true ) . ‘</pre>’ … Read more

how to add Custom menu item like User name in Header Menu only

wp_nav_menu_items filters passes menu arguments as second parameter. So if you modify code to this: add_filter( ‘wp_nav_menu_items’, ‘my_custom_menu_item’, 10, 2); function my_custom_menu_item($items, $args) … You will be able to check data $args inside function to match specific menu. However the specifics of what to check for depend on how precisely your menus are set up. … Read more

How to display the title for each menu item in a span attribute

When calling the wp_nav_menu() function you can pass the <span> tags in the link_before y link_after arguments: wp_nav_menu(array(‘link_before’ = > ‘<span>’, ‘link_after’ => ‘</span>’)); To have differents attributes in the span for each menu item you may need to use a custom nav_walker. For example, the next custom Nav_Walker is the default from WordPress, I’ve … Read more

Menu fallback “menu_class” rendering a “div” instead of a “ul”

First, I think you’re confusing the wp_nav_menu() args. The ‘menu_class’ parameter defines the class added to the menu element, which by default is <ul>, as per the ‘items_wrap’ parameter. The default ‘menu_class’ is ‘menu’, which results in <ul class=”menu”>. The real issue is actually the fallback_cb – the callback used when no menu is defined … Read more

wp_nav_menu not highlighting the current category when viewing a single post

There are several built-in CSS classes that come with the wp_nav_menu. In your case, you are looking for something like a current category class, which is not included. But we can easily add that to the CSS classes: function wpse_134409_current_category_class($classes, $item) { if ( is_single() && ‘category’ === $item->object && in_array($item->object_id, wp_get_post_categories($GLOBALS[‘post’]->ID)) ) $classes[] = … Read more

Use wp_nav_menu to display ALL pages

You could move away from wp_nav_menu() completely and instead use wp_list_pages(). See the codex for more information, but I believe this may be what you are looking for. http://codex.wordpress.org/Function_Reference/wp_list_pages I do understand that this function outputs the pages. The point I was trying to make though is that you can use this in conjunction with … Read more

wp_nav_menu add some element to container

This is fairly straightforward, assuming you have access to the php files. In the template that generates the menu, modify the wp_nav_menu call to specify container = FALSE, and to provide the container and additional div manually in the surrounding HTML. Without knowing your code in advance, one method would be to alter it as … Read more