You’re going about this wrongly, I think. The theme_location
parameter for wp_nav_menu()
is a template location – i.e. a physical location in the template, as defined/registered by the Theme. It’s not designed for an arbitrary number of locations based on arbitrary user content.
If you want to output category-specific menus in specific template locations, you would be better off to output your category-specific menu conditionally, with a fallback to a custom nav menu. (Or, better yet: conditionally output a user-assigned custom nav menu, with a fallback to your category menu.)
For example:
<div id="cat-menu">
<?php
// if the user has assigned a menu, use it
if ( has_nav_menu( 'cat-menu' ) ) {
wp_nav_menu( array(
'theme_location' => 'cat-menu'
) );
}
// otherwise, if this is a category archive index page,
// output a category menu
else if ( is_category() ) {
// output a list of categories
}
?>
</div>
That said, your issue is mainly a PHP/syntax problem. You need to build an array of menu locations, and then pass that built array to the register function.
// Base menu locations
$menu_locations = array(
'primary' => 'Main Navigation',
'secondary' => 'Footer Navigation'
);
// Get categories
$category_ids = get_all_category_ids();
// Loop through them
foreach( $category_ids as $cat_id ) {
// Get the Category object
$cat_obj = get_category( $cat_id );
// Get the Category Slug
$cat_slug = $cat_obj->slug;
// Concatenate it as a menu slug
$menu_slug = $cat_slug . '-menu';
// Get the Category Name
$cat_name = $cat_obj->name;
// Concatenate it as a menu name
$menu_name = $cat_name . ' Menu';
// Now add it to the array of menu locations
$menu_locations[$menu_slug] = $menu_name;
}
// Now register
register_nav_menus( $menu_locations );