I would really like to keep my template files as clean as possible, without including too much argument data. Is it possible with the following example...
$args = array(
'theme_location' => 'main-menu',
'menu_class' => 'list-inline',
'add_li_class' => 'list-inline-item'
);
wp_nav_menu($args);
...that I can store my array of arguments in a function within my functions.php file, and then simply call the argument data onto my header.php where my wp_nav_menu() lives? Please feel free to correct me if I am wrong, but is this a good time for me to use a add_action/do_action for this specific case?
I want to use my functions.php file as a one-stop-shop to (i.e. register the nav, add basic or advanced argument data to the same nav, etc..).
This is the first time I'm thinking about this, so I'm all for any best practices.
Many thanks!
I would really like to keep my template files as clean as possible, without including too much argument data. Is it possible with the following example...
$args = array(
'theme_location' => 'main-menu',
'menu_class' => 'list-inline',
'add_li_class' => 'list-inline-item'
);
wp_nav_menu($args);
...that I can store my array of arguments in a function within my functions.php file, and then simply call the argument data onto my header.php where my wp_nav_menu() lives? Please feel free to correct me if I am wrong, but is this a good time for me to use a add_action/do_action for this specific case?
I want to use my functions.php file as a one-stop-shop to (i.e. register the nav, add basic or advanced argument data to the same nav, etc..).
This is the first time I'm thinking about this, so I'm all for any best practices.
Many thanks!
a function like this:
function get_nav_args($overrides = array()) {
$defaults = array(
'theme_location' => 'main-menu',
'menu_class' => 'list-inline',
'add_li_class' => 'list-inline-item',
);
return shortcode_atts($defaults, apply_filters('some_custom_identifier_nav_menu_args', $overrides, $defaults) );
}
would be callable like wp_nav_menu(get_nav_args())
using shortcode atts and allowing an overrides array to be passed in would allow you to change the values if necessary without duplicating the whole thing. As for your put everything in functions.php I would avoid that if possible. Use include
or require
to include files that contain your functions. this way you can sort them out by type, use, location, or whatever else you'd like and won't end up digging through a single massive file looking for something down the road.
For 1st phase answer :
This function will also works :
if( !function_exists( 'menu_args' ) ) {
function menu_args() {
$args = array(
'theme_location' => 'main-menu',
'menu_class' => 'list-inline',
'add_li_class' => 'list-inline-item',
);
return $args;
} }
After defining the function, you can call it by :
wp_nav_menu( menu_args() );
For 2nd phase answer :
Functions.php is the essential file for wordpress that does not mean to have all the theme functions, tags/meta tags functions or custom meta, registering and defining own functions, etc on same file.
For beginner phase, its good to go with only functions.php but if you want to code at professional level, you must know how to organize the files. You can use include
or require
on the basis of requirements to include the different files.
What i recommend is, to make different files for different kinds of functions like you can make theme-functions.php that is related to theme, template-functions.php for template one and so on. You can include it to a folder or different one as per your requirements and convenient.
You have to maintain the hierarchy of files and folders so that you will not get confused of finding the codes even after gap of time.