How to include the ‘current-menu-ancestor’ class on a custom post type menu in WordPress?

This is the final working code: <?php function additional_active_item_classes($classes = array(), $menu_item = false){ global $wp_query; if(in_array(‘current-menu-item’, $menu_item->classes)){ $classes[] = ‘current-menu-item’; } if ( $menu_item->post_name == ‘product’ && is_post_type_archive(‘product’) ) { $classes[] = ‘current-menu-item’; } if ( $menu_item->post_name == ‘product’ && is_singular(‘product’) ) { $classes[] = ‘current-menu-item’; } return $classes; } add_filter( ‘nav_menu_css_class’, ‘additional_active_item_classes’, 10, … Read more

Bootstrap dropdown nav pills not working with wp_nav_menu()

I ran into similar problem when I created a bootstrap navigation menu and dropdown links were all showing up at once. I used the following steps to correct the issue: download navwalker from github repo link: https://github.com/wp-bootstrap/wp-bootstrap-navwalker put this file in the root folder of the theme and in the functions.php file userequire_once get_template_directory() . … Read more

Menu Disappears on Category Pages

I know this is an old question but seems that is still unresolved. As @Milo said you may have an incorrect pre_get_posts implementation. Most people do this like this (example): add_filter( ‘pre_get_posts’, ‘query_post_type’ ); function query_post_type( $query ) { if ( is_category() ) { $post_type = get_query_var( ‘post_type’ ); if ( $post_type ) { $post_type … Read more

Menu items (all menus) being deleted randomly on their own [duplicate]

Here are two loggers to help you to tackle the problem. 1) If you want to know when the number of nav menu items changes, you can use the following logger: /** * A logger for nav menu items count – writes to the nav.log file. * * @link http://wordpress.stackexchange.com/a/149394/26350 */ add_filter( ‘wp_get_nav_menu_items’, function( $items, … Read more

How show sub menu only using wp_nav_menu()

I’ve made a free plugin which solves this problem! https://wordpress.org/plugins/wp-nav-menu-extended/ This plugin extends the native wp_nav_menu function and adds additional options: level : (integer) (required for this plugin to work) The level of the navigation menu to show. If no child_of parameter is passed, it shows all the items of this level child_of : (string|integer) … Read more

Create a formatted table-like menu

I think your question is a perfect example for the XY Problem. In WordPress you do not create such a menu in a post editor. You use a menu. Once you start thinking about your problem from this point, everything is easy. 🙂 First register a custom navigation menu for this list in your theme’s … Read more

Determine if a navigation item has children

You can filter wp_nav_menu_objects and add the classes in one rush. You don’t even need a custom walker for that. add_filter( ‘wp_nav_menu_objects’, ‘add_has_children_to_nav_items’ ); function add_has_children_to_nav_items( $items ) { $parents = wp_list_pluck( $items, ‘menu_item_parent’); foreach ( $items as $item ) in_array( $item->ID, $parents ) && $item->classes[] = ‘has-children’; return $items; } The class has-children will … Read more

How to set limit only on top levels of wp_nav_menu?

According to the codex, you just need to use depthparameter to display only top-level pages : <?php wp_nav_menu( array( ‘location’ => ‘your_location’, ‘depth’ => 1 ) ); ?> For more reference see this. You could also fix both of your problem by using [wp_get_nav_menu_items][2] and then using a custom loop to parse only first and … Read more