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.

I'm still very novice at PHP so any help is greatly appreciated. Typically I have found the codex to be very helpful but it appears custom walkers may be outside its scope.

I'd like to have thumbnails show in a custom nav menu which I have in a theme. From what I understand I need to create a custom walker to accomplish this.

I've placed this

wp_nav_menu( array( 'container_class' => 'menu-stamp', 'theme_location' =>'stamp-menu' , 'walker' => new Thumbnail_Walker) ); 

in my theme menu location and inserted the 3 thumbnail lines below the item output section below

/*
 * Create HTML list of nav menu items.
 * Replacement for the native Walker, using the description.
 *
 * @see    http://wordpress.stackexchange.com/q/14037/
 * @author toscho, http://toscho.de
 */
class Thumbnail_Walker extends Walker_Nav_Menu
{
/**
 * Start the element output.
 *
 * @param  string $output Passed by reference. Used to append additional content.
 * @param  object $item   Menu item data object.
 * @param  int $depth     Depth of menu item. May be used for padding.
 * @param  array $args    Additional strings.
 * @return void
 */
 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>";

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

    // insert thumbnail
    // you may change this
    $thumbnail = '';
    if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
        $thumbnail = get_the_post_thumbnail( $id );
    }

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

    $item_output = $args->before
        . "<a $attributes>"
        . $args->link_before
        . $title
        . '</a> '
        . $args->link_after
        . $thumbnail
        . $args->after;

    // Since $output is called by reference we don't need to return anything.
    $output .= apply_filters(
        'walker_nav_menu_start_el'
    ,   $item_output
    ,   $item
    ,   $depth
    ,   $args
    );
   }
}

I get an error for unexpected } at the end of the thumbnail code so its wrong but I'm not sure why.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Add a semicolon ; after get_the_post_thumbnail( $id ):

get_the_post_thumbnail( $id );
share|improve this answer
    
@toscho doh! thank you, next question - i now get the following error Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, '$mytheme_walker::walk' was given in /public_html/wp-includes/nav-menu-template.php on line 473 –  javy May 2 '11 at 1:13
    
@javy See this answer for an example. Your code excerpt doesn’t show the cause of your problem. –  toscho May 2 '11 at 1:30
    
@toscho thanks again, i think that should help me find my way –  javy May 2 '11 at 1:42
    
wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'stamp-menu' , 'walker' => new Thumbnail_Walker) ); and $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 . "<a $attributes>" . $args->link_before . $title . '</a> ' . $args->link_after . $thumbnail . $args->after; is my code, still no thumbnails –  javy May 2 '11 at 12:43
    
Use get_the_post_thumbnail() to get the value. the_post_thumbnail() just prints it out not where you need it. –  toscho May 2 '11 at 12:52

For future visitors you need to change

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

to

  $thumbnail = '';
    if ( has_post_thumbnail( $item->object_id ) ) {
  $thumbnail = get_the_post_thumbnail( $item->object_id );
  }

the right answer was posted here: http://stackoverflow.com/questions/5893036/how-to-return-page-id-in-custom-walker

share|improve this answer

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.