Genesis framework将过滤器应用于wp\\u nav\\u menu()。我想做的是两件事。
1通过过滤器更改菜单类
2通过过滤器添加菜单id
下面是我试图添加过滤器的函数。
add_action( \'genesis_after_header\', \'genesis_do_nav\' );
/**
* Echoes the "Primary Navigation" menu.
*
* The preferred option for creating menus is the Custom Menus feature in
* WordPress. There is also a fallback to using the Genesis wrapper functions
* for creating a menu of Pages, or a menu of Categories (maintained only for
* backwards compatibility).
*
* Either output can be filtered via \'genesis_do_nav\'.
*
* @since 1.0.0
*
* @uses genesis_get_option() Get theme setting value
* @uses genesis_nav() Use old-style Genesis Pages or Categories menu
* @uses genesis_structural_wrap() Adds optional internal wrap divs
*/
function genesis_do_nav() {
/** Do nothing if menu not supported */
if ( ! genesis_nav_menu_supported( \'primary\' ) )
return;
if ( genesis_get_option( \'nav\' ) ) {
if ( has_nav_menu( \'primary\' ) ) {
$args = array(
\'theme_location\' => \'primary\',
\'container\' => \'\',
\'menu_class\' => genesis_get_option( \'nav_superfish\' ) ? \'menu menu-primary superfish\' : \'menu menu-primary\',
\'echo\' => 0,
);
$nav = wp_nav_menu( $args );
} elseif ( \'nav-menu\' != genesis_get_option( \'nav_type\', \'genesis-vestige\' ) ) {
$args = array(
\'theme_location\' => \'primary\',
\'menu_class\' => genesis_get_option( \'nav_superfish\' ) ? \'menu menu-primary superfish\' : \'menu menu-primary\',
\'show_home\' => genesis_get_option( \'nav_home\', \'genesis-vestige\' ),
\'type\' => genesis_get_option( \'nav_type\', \'genesis-vestige\' ),
\'sort_column\' => genesis_get_option( \'nav_pages_sort\', \'genesis-vestige\' ),
\'orderby\' => genesis_get_option( \'nav_categories_sort\', \'genesis-vestige\' ),
\'depth\' => genesis_get_option( \'nav_depth\', \'genesis-vestige\' ),
\'exclude\' => genesis_get_option( \'nav_exclude\', \'genesis-vestige\' ),
\'include\' => genesis_get_option( \'nav_include\', \'genesis-vestige\' ),
\'echo\' => false,
);
$nav = genesis_nav( $args );
}
$nav_output = sprintf( \'%2$s%1$s%3$s\', $nav, genesis_structural_wrap( \'nav\', \'open\', 0 ), genesis_structural_wrap( \'nav\', \'close\', 0 ) );
echo apply_filters( \'genesis_do_nav\', $nav_output, $nav, $args );
}
}
我最初尝试添加\\u filter,在这里我可以修改$args来更改类并添加id。
我该怎么做呢?
这是我的“最佳猜测”
function xyz_modify_nav($args) {
$args = array(
\'menu_class\' => \'xyz\',
\'menu_id\' => \'custom\'
);
}
add_filter(\'genesis_do_nav\', \'xyz_modify_nav\',20);
最合适的回答,由SO网友:Daniel Zimmermann 整理而成
Evan代码的新更新版本与Genesis 1.9配合使用,修复了显示所有导航项的错误。
/*
* Add classes to Genesis Navigation
* Tested with Genesis 1.9 (Beta)
* 29.12.2012
*/
add_filter( \'genesis_do_nav\', \'override_do_nav\', 10, 3 );
function override_do_nav($nav_output, $nav, $args) {
$args[\'menu_id\'] = \'the_id_you_want\';
$args[\'menu_class\'] = \'class1 class2\'; // replace what was there
$args[\'menu_class\'] .= \' nav-bar\'; // or append to it
if ( genesis_get_option( \'nav\' ) ) {
if ( has_nav_menu( \'primary\' ) ) {
$nav = wp_nav_menu( $args );
} elseif ( \'nav-menu\' != genesis_get_option( \'nav_type\', \'genesis-vestige\' ) ) {
$nav = genesis_nav( $args );
}
}
return sprintf( \'<div id="nav">%2$s%1$s%3$s</div>\', $nav, genesis_structural_wrap( \'nav\', \'open\', 0 ), genesis_structural_wrap( \'nav\', \'close\', 0 ) );
}
干杯葡萄酒
SO网友:Evan Mattson
Genesis提供给您使用的函数不允许您在wp_nav_menu
使用它们调用函数。过滤器只会将它们传递给过滤器回调函数以供使用。
Updated Solution
<我最初的回答太冗长了。这要简单得多,并且应该完全按照您的要求执行
add_filter( \'genesis_do_nav\', \'override_do_nav\', 10, 3 );
function override_do_nav($nav_output, $nav, $args) {
$args[\'menu_id\'] = \'the_id_you_want\';
$args[\'menu_class\'] = \'class1 class2\'; // replace what was there
$args[\'menu_class\'] .= \' class3\'; // or append to it
// check which function should be used to build the nav
// rebuild the nav using the updated arguments
if(array_key_exists(\'type\', $args))
$nav = wp_nav_menu( $args );
else
$nav = genesis_nav( $args );
// return the modified result
return sprintf( \'%2$s%1$s%3$s\', $nav, genesis_structural_wrap( \'nav\', \'open\', 0 ), genesis_structural_wrap( \'nav\', \'close\', 0 ) );
}