您无需编写自定义助行器即可实现这一点。您可以修改wp_nav_menu()
函数并连接到相应的默认导航菜单过滤器中,以修改各个菜单项的输出。E、 g.(假设您的主题菜单位置称为主菜单)
把这个放在你的函数中。php文件
/**
* Output Custom Menu
*
* Call this function within your template
* files wherever you want to output your
* custom menu.
*
*/
function custom_theme_menu() {
$menu = wp_nav_menu( array(
\'theme_location\' => \'primary\',
\'container_class\' => \'menu\',
\'items_wrap\' => \'%3$s\', // needed to remove the <ul> container tags
\'fallback_cb\' => false,
\'echo\' => false
) );
// Strip out the <li> tags from the output.
echo strip_tags( $menu,\'<a>, <div>\' );
}
/**
* Add Nav Menu Item Link Classes
*
* Detects if we are rendering the custom menu
* and adds the appropriate classnames.
*
*/
function custom_theme_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
// For all other menus return the defaults.
if ( $args->theme_location !== \'primary\' ) {
return $atts;
}
// Add the link classes.
$class_names = array( \'some-class\' );
if ( $item->current ) {
$class_names[] = \'is-active\';
}
$atts[\'class\'] = join( \' \', $class_names );
return $atts;
}
add_filter( \'nav_menu_link_attributes\', \'custom_theme_nav_menu_link_attributes\', 10, 4 );
然后,在模板文件中,只要调用以下函数即可输出菜单:
custom_theme_menu();