Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development Building Out WordPress Navigation Coding A Basic Navigation in WordPress

David Halsell
David Halsell
1,730 Points

Why use $defaults = array(... instead of wp_nav_menu( array(... ?

What's the difference between the two menu functions below? Why use one instead of the other?

$defaults  =  array(
  'theme_location' => 'primary'
wp_nav_menu( 'defaults' );


wp_nav_menu( array( 'theme_location' => 'primary' ) );

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

The only difference is for us humans. If you had a few options you wanted to pass to a function, the top example makes more sense as it would be more readable to a human.

If you only had one or two options to pass in, I'd just use the bottom example.

Both execute the same way.

NOTE your top example has a semantic flaw. You would pass in the $defaults variable, so it would be wp_nav_menu( $defaults );

David Halsell
David Halsell
1,730 Points

Ah, figured that was the case. They seemed to me functionally identical. Thanks for catching the error!