• I’m trying to create a custom meta box that has a dropdown select list of all user-created custom menus. I think I’m close but I can’t get the list of menus to “echo” the way I need it to. Here’s what I have so far…

    $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
    
    foreach ( $menus as $menu ) {
    // this echoes a list of all the menus with a comma behind them...
    echo $menu->name . ', ';
    }
    
    // Adding sub menu drop down to pages.
    
    $meta_boxes[] = array(
    	'id' => 'sub_menu',
    	'title' => 'Sub Menu',
    	'pages' => array('page'),
    	'context' => 'normal',
    	'priority' => 'high',
    	'fields' => array(
    		array(
    			'name' => 'Select Sub Menu',
    			'desc' => 'The sub menu for this page created via Appearance > Menus',
    			'id' => $prefix . 'custom_menu',
    			'type' => 'select',
    			'options' => // WHAT WILL WORK HERE???
    
    		)
    	)
    );

    I’ve tried to save the data in an array and output that in the “options” param but when I var_dump $menus, It’s full of all sorts of data, not just the menu names and thus it throws errors. The format needs to be…

    'options' => array('Option 1', 'Option 2', 'Option 3')

    I also had no luck with the implode() function since there’s so much extra data in $menus that I don’t need which throws errors.

    My lack of skills with arrays is hurting progress. Any thoughts are appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter shagdirty

    (@shagdirty)

    I ended up figuring out a way to get this accomplished for those who may be curious. I’m not sure if this is the most efficient way to do it or not but it works…

    function makeMyMenus() {
    	$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
    	global $menuArray;
    	$menuArray = array('select a sub menu');
    
    	foreach ( $menus as $menu ) {
    		$menuArray[] = $menu->name;
    	}
    }
    makeMyMenus();
    
    // Adding sub menu drop down to pages.
    
    $meta_boxes[] = array(
    	'id' => 'sub_menu',
    	'title' => 'Sub Menu',
    	'pages' => array('page'), // applicable post types
    	'context' => 'side',
    	'priority' => 'low',
    	'fields' => array(
    		array(
    			'name' => 'Sub Menu',
    			'desc' => 'The sub menu for this page created via Appearance > Menus',
    			'id' => 'custom_menu',
    			'type' => 'select',
    			'options' => $menuArray
    		)
    	)
    );

    Use it on the front-end something like this…

    if( get_post_meta($post->ID, 'custom_menu', true) == true ) {
    $menuName = get_post_meta($post->ID, 'custom_menu', true);
    }
    
    wp_nav_menu( array('menu' => $menuName));

    I was just wondering, did you get this to work? I tried using that code in my functions file and it did not show a meta box.
    Maybe it is the newer version 3.1 wordpress.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying a List of Custom Menus in Custom Meta Box’ is closed to new replies.