0

Genesis framework applies filters to the wp_nav_menu(). What I want to do is 2 things.

1 change the menu class via a filter

2 add a menu id via a filter

Here is the function I am trying to add a filter to.


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

}

I was initially trying to add_filter where I can modify the $args to change the class and add an id.

How do I go about doing this?

Here is my 'best guess'


function xyz_modify_nav($args) {
    $args = array(
       'menu_class' => 'xyz',
       'menu_id'    => 'custom'
    );
}
add_filter('genesis_do_nav', 'xyz_modify_nav',20);

2 Answers 2

4

This new updated version of Evan's Code works with Genesis 1.9 and fixes the bug of displaying all navigation items.

/*
 * 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 ) ); 
}

Cheers Daniel

1
  • Tuned it slightly to my needs, works great.
    – fischi
    Commented Dec 29, 2012 at 15:39
2

The function that Genesis gives you to use does not give you the option of altering those args before the wp_nav_menu function is called with them. The filter merely passes those to your filter callback function to use.

Updated Solution

My original answer was much too long-winded. This is much simpler and should do exactly what you want.

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

}
1
  • Great! Glad to help. Commented Jul 13, 2012 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.