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.

created this walker below to create a set of tabs. All looks ok but in the source there are several more than there should be causing validation errors.

I only want to generate a tab for 'question' post types.

code -

        $mmArgs  = array(
          'menu'            => 'competitionmenu', 
          'container'       => 'div', 
          'container_class' => 'summary_chart_tab_icons', 
          'menu_id' => 'summary_chart_tab_icons',
          'menu_class'      => 'summary_chart_tab_icons clearfix', 
          'echo'            => true,
          'walker' => new summary_walker_2013_TABS()
    );

    wp_nav_menu($mmArgs);

class summary_walker_2013_TABS extends Walker_Nav_Menu{

function start_el(&$output, $item, $depth, $args){

    global $wp_query, $current_user;
    $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

    $class_names = $value = '';

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

    $voted = hasVoted($item->object_id,$current_user->ID);
    $voteMade = $voted[0];

    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    $class_names =  ($voted ? ' voted' : '');
    $icon_state=  ($voted ? '-voted' : '');

    $class_names = ' class="tab-icon '.the_slug($item->object_id). esc_attr( $class_names ) . ' clearfix " ';

    $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        ) .'"' : '';

    $icon_name = child_template_directory. '/images/images/tab-icon-'.the_slug($item->object_id).$icon_state;

    if($item->object=='question' ) : 

        $question = get_post($item->object_id);

        $output .= '<li data-default-icon="'.$icon_name.'.gif" data-active="'.$icon_name.'-active.gif" data-active-voted="'.$icon_name.'-active.gif" style="background-image: url('.$icon_name.'.gif)" id="tab_'.$question->post_name.'"  ' . $class_names .'>';

        $output .='</li>';

        $item_output .=  ''.$args->after;

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

    endif;  

}

}

and extra closing li s are in twos before each

  • . I think it might be the menu items that aren't 'questions' coming through but how can i omit them in a walker?

    best, Dc

  • share|improve this question
    add comment

    1 Answer

    up vote 1 down vote accepted

    Remove the line $output .='</li>'; from your walker. The closing </li> is automatically entered by wordpress for every item. This is done in end_el function of the Walker_Nav_Menu class.

    share|improve this answer
        
    cheers. That helped but i can see now its adding </li> even for post_types that aren't 'questions'. Is there a way i can truly reduce using if($item->object=='question' ) : or similar somehow? Dc –  daniel Crabbe Nov 22 '12 at 11:52
        
    In your class, create this other function function end_el(&$output, $item, $depth = 0, $args = array()){$output .= "</li>\n";} & modify it as needed –  Mridul Aggarwal Nov 22 '12 at 12:29
        
    ace thanks - starting to understand the walkers a bit better now! Final code function end_el(&$output, $item, $depth = 0, $args = array()){ if($item->object=='question' ) : $output .= "</li>\n"; endif; } –  daniel Crabbe Nov 22 '12 at 13:15
    add comment

    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.