How to avoid wp_nav_menu() ID conflict?

The solution is not to call the same ‘theme_location’ more than once. Theme location is intended to represent an explicit location within the template. Just register a separate ‘theme_location’ for each separate location within the template that you want to display a nav menu. Consider your chosen ‘theme_location’ names to be semantic names, representing the … Read more

Making breadcrumb with wp_nav_menu

I couldn’t believe there is not a single FREE plugin available that does this. So I wrote my own function. Here you go. Just copy this to your functions.php: function my_breadcrumb($theme_location = ‘main’, $separator=” > “) { $theme_locations = get_nav_menu_locations(); if( ! isset( $theme_locations[ $theme_location ] ) ) { return ”; } $items = wp_get_nav_menu_items( … Read more

how to create a menu with all sub categories?

This depends on what kind of menu you are talking about: 1) If you are talking about “custom menus” (found in the Backend under Design -> Menus) you can do the following: Create a new function with the action hook add_category inside of this function, you can create a new post of type the menu … Read more

Check if page is in a certain menu

Here’s the function I wrote to figure this out. You give it a menu slug/name/ID and post/page ID and it returns TRUE if that post/page is in the specified menu and FALSE otherwise. Then it was just simply a matter of a quick if/else statement to check against the two menus and display the correct … Read more

add custom class to wp_nav_menu using filter hook nav_menu_css_class

here is a simply example: add_filter(‘nav_menu_css_class’, ‘auto_custom_type_class’, 10, 2 ); function auto_custom_type_class($classes, $item) { if ($item->type_label == “CUSTOM_TYPE_NAME”){ $classes[] = “New_Class”; } return $classes; } just change CUSTOM_TYPE_NAME to the name of your custom post type and New_Class with the name of your class and paste this snippet in your theme’s functions.php file.

Adding category ID or slug to WP Nav Menu

Use the nav_menu_css_class filter to add classes to wp_nav_menu output. Add ID (no additional query needed): function wpa_category_nav_class( $classes, $item ){ if( ‘category’ == $item->object ){ $classes[] = ‘menu-category-‘ . $item->object_id; } return $classes; } add_filter( ‘nav_menu_css_class’, ‘wpa_category_nav_class’, 10, 2 ); Add slug (loads category object via get_category): function wpa_category_nav_class( $classes, $item ){ if( ‘category’ … Read more

Adding line breaks to nav menu items

Following the hint from @Rarst regarding safe characters here’s what I ended up doing: function wpa_105883_menu_title_markup( $title, $id ){ if ( is_nav_menu_item ( $id ) && ! is_admin() ){ $title = preg_replace( ‘/#BR#/’, ‘<br/>’, $title ); } return $title; } add_filter( ‘the_title’, ‘wpa_105883_menu_title_markup’, 10, 2 ); Edit: Also per Rarst’s comment I’ve replaced the preg_replace … Read more