I am trying to use a custom walker to display thumbnails of pages with custom navigation menu items. My complete walker below seems to be this section
$thumbnail = '';
if( $id = has_post_thumbnail( (int)$item->object_id ) ) {
$thumbnail = get_the_post_thumbnail( $id );
}
does not pull the page id, and therefore I do not get thumbnails. any help is appreciated
class Thumbnail_Walker 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>";
$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 = 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;
$output .= apply_filters(
'walker_nav_menu_start_el'
, $item_output
, $item
, $depth
, $args
);
}
}
source
share