Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to edit or apply a filter to the default Genesis navigation genesis_do_nav

Currently I'm extending a Walker class to add a <span> with data-attribute around the navigation's link text.

class Menu_With_Data_Attr extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';

        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

        $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
        $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
        $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'><span data-hover="'.$item->title.'">';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</span></a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

How might I apply that walker to the output of the genesis_do_nav?

share|improve this question

1 Answer 1

up vote 2 down vote accepted
+50

You could try to use the wp_nav_menu_args filter (untested):

/**
 * Add the Menu_With_Data_Attr walker to the wp_nav_menu() used by genesis_do_nav()
 */
add_filter( 'wp_nav_menu_args', function( $args ){

    if(    isset( $args['menu_class'] ) 
        && 'menu genesis-nav-menu menu-primary' === $args['menu_class'] )
    {
        if( class_exists( 'Menu_With_Data_Attr' ) )
        {
            $args['walker'] = new Menu_With_Data_Attr(); 
        }
    }

    return $args;

});

to add the Menu_With_Data_Attr walker to the wp_nav_menu() used by genesis_do_nav().

share|improve this answer
    
That worked. Thank you for the assistance. Not really sure why the question was down-voted though. –  NW Tech Jan 6 at 23:22
    
Happy to hear it worked for you. I'm not sure either, why your question was downvoted. –  birgire Jan 6 at 23:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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