Support » Plugins » Hacks » Customizer – Controls on Primary Menu

  • I’m trying to add some custom controls to only the Primary Menu. I can get the control to show up when on a specific menu, albeit not very pretty since it butts right up against the menu items but it still adds it:

    function theme_customizer_controls( $customizer ) {
    	// Arbitrary Control
    	$customizer->add_setting( 'theme_something', array(
    		'type'				=> 'theme_mod',
    		'transport'			=> 'refresh',
    		'sanitize_callback'	=> 'sanitize_text_field',
    	) );
    	$customizer->add_control( 'theme_something', array(
    		'label'			=> __( 'Something', 'theme' ),
    		'section'		=> 'nav_menu[2]',
    		'settings'		=> 'theme_something',
    		'type'			=> 'text',
    	) );
    add_action( 'customize_register', 'theme_customizer_controls' );

    The problem is that the section slug appears to have an array index so it would be difficult to tell if the primary menu is at index 2, 3, or 10. Is there a way to only show this control whenever viewing the Primary Menu?

    I’m not opposed to using JS to hide / show the controls either if that’s an option.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    A complete guess, but I’ll bet the index is related to the list of locations the theme defines when it registers locations. This data is stored in options under “theme_mods_{theme_slug}”. There are several mods stored in an array, you want the key “nav_menu_locations”. The corresponding value is an array of registered locations. I’m pretty sure these occur in the same order that you registered them.

    Probably the first location corresponds to “nav_menu[0]”, the second to [1], etc. This doesn’t tell us what menu is being used, only the registered location. The menu any nav_menu_item is assigned to is done through a custom “nav_menu” taxonomy. Each menu item is assigned a term corresponding to the menu name the theme or user selects or creates on the menu admin screen. Which menu term is used at what location is the value corresponding to the location key in the “nav_menu_locations” array, where the value is the term ID.

    I’m not quite sure how to directly answer your question, but the above background information should hopefully allow you to determine your own answer.

Viewing 1 replies (of 1 total)
  • The topic ‘Customizer – Controls on Primary Menu’ is closed to new replies.