I have the following code:
add_filter( 'wp_nav_menu_objects', 'dynamically_add_shop_categories_to_submenu' );
function dynamically_add_shop_categories_to_submenu( $items ) {
$taxonomy_name = array(
'shop-category'
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
'exclude' => '160',
);
$terms = get_terms( $taxonomy_name, $args );
$base="http://" . $_SERVER['HTTP_HOST'] . '/businesses/';
$bc="&shop-category=";
$bt="?business-type=shops";
foreach ( $terms as $term ) {
$shops = array (
'title' => $term->name,
'menu_item_parent' => 1157,
'ID' => '',
'db_id' => '',
'url' => $base . $bt . $bc . $term->slug
);
$items[] = (object) $shops;
}
return $items;
}
This successfully adds submenu items from the ‘shop-category’ taxonomy to a top level menu with the ID 1157. This works as expected.
There are more top level menus which remain unaffected by this, which is correct, as we’ve only specified these items to be added to the 1157 menu.
However, at the footer of our site we have three more menus, all of which get these submenus added. Why is the code doing that? The menus don’t appear to have any IDs, so how do we modify the code to exclude them?