admin管理员组文章数量:1290500
I have a main menu of 3 items, and for each of these I have a different secondary menu. I would like to show the related secondary menu on each page. I currently used is_page with slug. I will need to add wpml, so each page will have more slugs. I would like to ask if you can suggest me a cleaner and more maintainable solution. This is what I did at the moment:
if ( is_page( 'page-1' ) ) {
wp_nav_menu(
array(
'theme_location' => 'secondary-menu-1'
)
);
} elseif (is_page( 'page-2' ) ) {
wp_nav_menu(
array(
'theme_location' => 'secondary-menu-2'
)
);
}
I have a main menu of 3 items, and for each of these I have a different secondary menu. I would like to show the related secondary menu on each page. I currently used is_page with slug. I will need to add wpml, so each page will have more slugs. I would like to ask if you can suggest me a cleaner and more maintainable solution. This is what I did at the moment:
if ( is_page( 'page-1' ) ) {
wp_nav_menu(
array(
'theme_location' => 'secondary-menu-1'
)
);
} elseif (is_page( 'page-2' ) ) {
wp_nav_menu(
array(
'theme_location' => 'secondary-menu-2'
)
);
}
Share
Improve this question
asked Jun 15, 2021 at 23:26
Vinz ClorthoVinz Clortho
451 silver badge5 bronze badges
1 Answer
Reset to default 0This is just sort of a PHP shortcut, but you could create a mapping describing which conditional values map to what menu locations, and loop through it testing each condition and printing menus for matches:
/**
* Prints wp_nav_menu(s) by matching conditional function arguments to menu locations.
*
* @param array $locations A mapping of predicate function names to associative
* arrays mapping predicate function arguments to menu
* locations.
* @param array $args Optional. An associative array of arguments to pass on to
* wp_nav_menu().
* @param boolean $multiple Optional. Whether or not to allow multiple menus to
* match/print.
* @return string|void The menu(s) markup, if $args['echo'] is set to false.
**/
wpse390599_conditional_nav_menu( $locations, $args = [], $multiple = false ) {
$conditional_locations = $locations;
$markup = '';
$return = isset( $args['echo'] ) && $args['echo'] === false;
foreach( $conditional_locations as $predicate => $locations ) {
foreach( $locations as $condition => $location ) {
if( ! call_user_func( $predicate, $condition ) )
continue; // No match - skip location.
// Predicate conditional returned TRUE - print the menu
$args['location'] = $location;
$markup = wp_nav_menu( $args );
// If we're not facilitating multiple matching menus, bail out.
if( ! $multiple ) {
// Return the markup if `$args['echo'] is false, per wp_nav_menu() convention.
if( $return )
return $markup;
return;
}
}
}
// If multiple menus were matched and $args['echo'] is false, return the markup.
if( $return )
return $markup;
}
Usage:
wpse390599_conditional_nav_menu(
[
'is_page' => [
'page-1' => 'secondary-menu-1',
'page-2' => 'secondary-menu-2',
'' => 'secondary-menu-default',
],
]
);
As an added benefit, when $multiple
is false
, the ordering of the $conditional_locations
arrays can be used to specify precedence in the case that multiple conditions would match.
Whether or not this is more maintainable than your current solution really depends on the specifics of the use-case. Of note, this structure would not support WordPress conditional predicates which take multiple arguments, e.g. is_tax( $taxonomy, $term )
.
本文标签: navigationConditional secondary menus
版权声明:本文标题:navigation - Conditional secondary menus 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741500581a2382035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论