WordPress.org

Ready to get started?Download WordPress

Forums

Custom walker for wp_nav_menu (1 post)

  1. virtous
    Member
    Posted 4 months ago #

    Hi there,
    I'm new to the WP forums and not sure if this is the right place for this post, my apologies if it is.

    Description of problem:
    I'm trying to output a set of menu items in my footer. Not all, just the ones that belong to a specific parent category. I use the wp_nav_menu() function as shown below:

    $args = array(
    	'echo'  	 => false,
    	'depth'		 => 0,
    	'container'  => false,
    	'items_wrap' => '',
    	'before' 	 => '',
    	'walker'	 => new BV_Custom_Nav_Footer
    );
    echo wp_nav_menu($args);

    Problem is that this function outputs its own html. So I'm trying to make my own walker class.

    Basically I want the follow HTML markup:

    <a href="to1">Link 1</a>
    <a href="to2">Link 2</a>
    <a href="to3">Link 3</a>

    So no wrapper around it, no divs, ul or li. Just the tags with the menu links.

    At the moment I have this code in my functions.php file

    class BV_Custom_Nav_Footer extends Walker_Nav_Menu {
    
    	function start_lvl( &$output, $depth = 0, $args = array() ) {
    
    	}
    
    	function end_lvl( &$output, $depth = 0, $args = array() ) {
    
    	}
    
    	function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
    		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
    
    		$class_names = '';
    
    		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
    		$classes[] = 'menu-item-' . $item->ID;
    
    		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
    		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
    
    		$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
    		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
    
    		$output = "";
    
    		$atts = array();
    		$atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
    		$atts['target'] = ! empty( $item->target )     ? $item->target     : '';
    		$atts['rel']    = ! empty( $item->xfn )        ? $item->xfn        : '';
    		$atts['href']   = ! empty( $item->url )        ? $item->url        : '';
    
    		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
    
    		$attributes = '';
    		foreach ( $atts as $attr => $value ) {
    			if ( ! empty( $value ) ) {
    				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
    				$attributes .= ' ' . $attr . '="' . $value . '"';
    			}
    		}
    
                    $item_output = $args->before;
    		$item_output .= '<a'. $attributes .'>';
    		/** This filter is documented in wp-includes/post-template.php */
    		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    		$item_output .= '</a>';
                    $item_output .= $args->after;
    
    		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    	}
    
    	function end_el( &$output, $item, $depth = 0, $args = array() ) {
    
    	}
    }

    This gives me a PHP Notice: Trying to get property of non-object for lines 49, 52 and 54 which are:

    line 49:
    $item_output = $args->before;

    line 52:
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;

    line 54:
    $item_output .= $args->after;

    I'm trying to understand how the Walker_Nav_Menu class works but it's tricky... so I guess any help is appreciated.

    ------- UPDATE --------

    This is really odd but the $args that are given in the wp_nav_menu is an array, not an object. The walker wouldn't know what to do with an array... right?

    Anyway, converting it to an object at the top of the start_el function made all the errors go away...

    $args = (object) $args;

Reply

You must log in to post.

About this Topic