Editing or filtering the output of the Genesis navigation

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.

Read More
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?

Related posts

1 comment

  1. 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().

Comments are closed.