• Hi,
    I extended the Walker class to get a custom menu option, but some troubles are giving me headache like hell.

    This is my walker start_el method:

    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 ) . ' hvr-bounce-to-top "';
    
              $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 .'>';
    
              $item_output .=
                                $args->link_before .
                                apply_filters( 'the_title', $item->attr_title, $item->ID );
    
              $item_output .= $args->link_after; 
    
              $item_output .= '</a>';
    
              $item_output .= $args->after;
    
              $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    
        }//start_el

    – $item objects
    This function doesn’t show any menu link because of $item.
    If I treat it like a WP_Post object it works fine. For example if I use:

    $attributes .= ( get_permalink( $item->ID ) ) ? ' href="' . esc_attr( get_permalink( $item->ID ) ) .'"' : '';
    it works good.
    The fact is that the official wordpress codex and lots of link in the internet, doesnt’ treat $item as a WP_object but seems like everything work for them.
    Why? How should I treat it?

    – home link trouble:
    If I can make it working treating $item as a WP_Object, I got problems with the home link anyway.
    The walker class doesn’t add the “hvr-bounce-to-top” class to the home link element and I can’t edit it.
    It’s like the Walker class can’t see it. I tried to var_dump the $item and $object variables but there’s nothing referred to the home link.
    How can I style it? How can I edit the home item?

    Thank you in advance for not making my head blowing!

  • The topic ‘Walker menu $item type and home link troubles’ is closed to new replies.