admin管理员组文章数量:1130349
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!
Share Improve this question edited Jan 8, 2019 at 15:23 klewis asked Jan 8, 2019 at 15:09 klewisklewis 8991 gold badge14 silver badges32 bronze badges 3- Is this theme entirely for your own purposes, or is it intended for distribution? – Milo Commented Jan 8, 2019 at 18:41
- ultimately for distribution. Is there something for me to consider? – klewis Commented Jan 8, 2019 at 18:42
- 1 If someone wants to make changes to your theme that will survive an update, the recommended way is via a child theme. This would let them override the template file that contains the menu and make changes that way, but if your args are stored in a file that can't be overridden, then you might want to use something like a filter instead, like this answer. – Milo Commented Jan 8, 2019 at 18:49
2 Answers
Reset to default 1a 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.
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!
Share Improve this question edited Jan 8, 2019 at 15:23 klewis asked Jan 8, 2019 at 15:09 klewisklewis 8991 gold badge14 silver badges32 bronze badges 3- Is this theme entirely for your own purposes, or is it intended for distribution? – Milo Commented Jan 8, 2019 at 18:41
- ultimately for distribution. Is there something for me to consider? – klewis Commented Jan 8, 2019 at 18:42
- 1 If someone wants to make changes to your theme that will survive an update, the recommended way is via a child theme. This would let them override the template file that contains the menu and make changes that way, but if your args are stored in a file that can't be overridden, then you might want to use something like a filter instead, like this answer. – Milo Commented Jan 8, 2019 at 18:49
2 Answers
Reset to default 1a 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.
本文标签: menusHow to reference argument data for wpnavmenu() from functionsphp
版权声明:本文标题:menus - How to reference argument data for wp_nav_menu() from functions.php 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749031864a2305803.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论