WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to display thumbnail on navigation (using walker) but it doesn't display in the right place. Thumbnails displays only in index.php, before the navigation links and all these thumbnails are same.

class Footer_Navwalker extends Walker_Nav_Menu {

 function start_el(&$output, $item, $depth, $args) {
  $classes = empty ( $item->classes ) ? array () : (array) $item->classes;

  $class_names = join(
  ' ',
  apply_filters(
      'nav_menu_css_class',
      array_filter( $classes ),
      $item
    )
  );

  ! empty ( $class_names )
    and $class_names = ' class="'. esc_attr( $class_names ) .'"';

  $output .= "<li id='menu-item-$item->ID' $class_names><div class='col-lg-3'>";

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

  $thumbnail = '';
  if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
    $thumbnail = the_post_thumbnail( $id );
  }

  $title = apply_filters( 'the_title', $item->title, $item->ID );

  $item_output = $args->before;
    $output .= "<a $attributes class='title'>";
    $output .= $args->link_before;
    $output .= $title;
    $output .= '</a> ';
    $output .= $args->link_after;
    $output .= $thumbnail->link_after;
    $output .= $args->after;
    $output .= '</div>';
 }
}
share|improve this question

the_post_thumbnail() echos content. This is explicitly stated in the Codex, and the function name follows the WordPress convention (mostly) that function names starting with the_ echo content.

Display the Featured Image (previously called Post Thumbnails) for the current post, as set in that post's edit screen.

The thumbnail will display as soon as that line in the walker executes. The thumbnail markup won't be concatenated into your string as it should be.

You need get_the_post_thumbnail() instead, which follows the WordPress convention that functions starting with get_ return content. Many, but not all, the_ functions have get_ sibling functions.

share|improve this answer

Your Answer

 
discard

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