0

I am building a theme in Wordpress where I need to register menus on the go.

The code below works just fine, but it's static (in functions.php)

function register_my_menus() {
register_nav_menus(

  array(
  'header-menu' => __( 'Header Menu' ),
  'footer-menu' => __( 'Footer Menu' ),
  'left-menu' => __( 'Left Menu' )
  )                 
)};

However, I have my menu list stored in the array below. How do I use this in register_nav_menus above? I tried to run a foreach, but it's not so easy to do that inside of an array, right?

$list_of_menus[];

The array above contains the following:

'fdsfds_fds' => __( 'fdsfds fds' ),
'Its_my_life' => __( 'Its my life' ),
'header-menu' => __( 'Header Menu' ),
'footer-menu' => __( 'Footer Menu' ),
'left-menu' => __( 'Left Menu' )

Thank you!

2
  • 1
    Belongs on wordpress.stackexchange.com Commented Oct 26, 2013 at 19:50
  • register_nav_menus($list_of_menus); - assuming your list is in the proper format, of course. Commented Oct 26, 2013 at 19:51

1 Answer 1

1

Hardly seems worthy of a full answer, but here goes:

If your array is:

$list_of_menus = array(
    'fdsfds_fds' => __( 'fdsfds fds' ),
    'Its_my_life' => __( 'Its my life' ),
    'header-menu' => __( 'Header Menu' ),
    'footer-menu' => __( 'Footer Menu' ),
    'left-menu' => __( 'Left Menu' )
);

Then registering is a matter of:

register_nav_menus($list_of_menus);

Of course, this may depend on how you are loading $list_of_menus - can you load it in time / before the register_nav_menus call has to be made?

Edit
There's a few ways to build your array. Above is the first. Further, you can add elements to this array like so:

$slug = "my_menu_slug';
$name = __('My Menu Name');

$list_of_menus[$slug] = $name;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I guess that solves half my problem. Now the real problem is that, as I'm new to arrays, is how to register them properly. I had each line as a string, but that's wrong! So I tried with $regarr[] = $slug => $rname;, but that's not allowed either. So, how do I write an array properly? Any help would be much appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.