• I’m trying to get the registered navigation menus for a certain blog in my network, but it only returns the navigation menus for the main blog, no matter what I do. Here is my code:

    switch_to_blog($blog_id);
    $menus = get_registered_nav_menus();
    var_dump($menus); //Always displays the menu locations for the main blog
    restore_current_blog();

    This works for other functions. Why doesn’t it work for this one, and is there a workaround?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    Where are you defining the blog ID? Did you call the globals already?

    Thread Starter Samer Bechara

    (@arbet01)

    The blog ID is passed through a loop – I’ve checked that this is correctly set.

    What do you mean by call globals? This is part of a plugin code. Can you clarify?

    IMO, this will never work. You will always get the nav_menus from the current blog. Sorry I do not know of any work-around. You would need to find a way to load theme via WP APIs.

    Thread Starter Samer Bechara

    (@arbet01)

    jkhongusc, can you explain to me why this won’t work? I’m just trying to understand the logic behind it, and it doesn’t seem to make sense.

    For anyone looking for a solution, here is my workaround:

    switch_to_blog($blog_id);
    
    	// Get registered menu locations via get_option
    	$theme_options = get_option('theme_mods_'. get_stylesheet());
    
            // Get theme menu locations being used
    	$theme_menu_locations = $theme_options['nav_menu_locations'];
    
            // Dump menu locations
    	var_dump($theme_menu_locations);
    
    	restore_current_blog();

    The only downside is that you only get the menu locations with a menu attached to them. For example, if you had two menu locations in your theme, and only one was being used, the other will not show here.

    However, I didn’t need more than that, so it solved my problem.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    ?????? Advisor and Activist

    $blog_id isn’t always accessible, which is why you need a global. But you seem to be okay ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Cannot get registered nav menus with switch to blog’ is closed to new replies.