使用过滤器修改Genesis wp_NAV_MENU

时间:2012-07-13 作者:shawn

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);

2 个回复
最合适的回答,由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 ) );

}

结束

相关推荐

Apply_Filters函数及其变量的说明

我正在学习如何使用PHP构建html表单,方法是以“simplr表单注册”插件为例。我正在查看以下代码:$form .= apply_filters(\'simplr-reg-instructions\', __(\'Please fill out this form to sign up for this site\', \'simplr-reg\')); 您能解释一下这里发生了什么吗?函数的作用是什么,为什么需要“simpr reg指令”和“simpr reg”?为什么这句话不能简单地说:$