2famous.TV
2famous.TV

Reputation: 470

Array in PhP for Wordpress / foreach loop in an array

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!

Upvotes: 0

Views: 663

Answers (1)

random_user_name
random_user_name

Reputation: 26160

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;

Upvotes: 1

Related Questions