add_filter( ‘wp_nav_menu_objects’, ‘ravs_add_menu_parent_class’,10,2 );
function ravs_add_menu_parent_class( $items, $args ) {
if ($args->menu == ‘main-menu’||$args->menu == ‘mobile-menu’) {
// add parent item
$item = array (
‘title’ => ‘Brands’,
‘menu_item_parent’ => 0,
‘ID’ => 999999999999876, //an unlikely, high number
‘db_id’ => 999999999999876, //an unlikely, high number
‘url’ => ‘#’
);
$new_links[] = (object) $item;
unset( $item );
// get woocommerce attribe call brands
$terms = get_terms( ‘pa_brand’, array(‘hide_empty’=>true) );
// add childs
foreach ($terms as $term) :
$term_link = home_url().’/shop/?filters=brand’.’%5B’.$term->term_id.’%5D’;
$label = $term->name;
$link = $term_link;
$item = array(
‘title’ => $term->name,
‘menu_item_parent’ => ‘999999999999876’,
‘ID’ => $term->term_id,
‘db_id’ => ”,
‘url’ => $link,
‘classes’ => array( ‘sub-menu-item’ )
);
// get the URL
$new_links[] = (object) $item; // Add the new menu item to our array
unset( $item );
endforeach;
$index = count( $items ) – 2; // Insert before the last two items
// Insert the new links at the appropriate place.
array_splice( $items, $index, 0, $new_links );
}
return $items;
}