I am trying to add the featured image of posts to my mega menu. I want to display the featured image inside a div tag. How would i add the featured image inside of a sprintf function?
$thumbnail = '';
if ( has_post_thumbnail( $item->object_id ) ) {
$thumbnail = the_post_thumbnail_url( $item->object_id );
}
class Walker_Nav_Primary extends Walker_Nav_menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
if ( array_search( 'menu-item-has-children', $item->classes )&& $depth==0 ) {
$output .= sprintf( "n<li class='topnavitem %s'><a href='%s' class="topnavtext">%s</a>n", ( array_search( 'current-menu-item', $item->classes ) || array_search( 'current-page-parent', $item->classes ) ) ? '' : '', $item->url, $item->title );
}
elseif ( array_search( 'menu-item-has-children', $item->classes )&& $depth==1 ) {
$output .= sprintf( "n<li class='productsnav %s'><a href='%s' class="productsnavtext">%s</a>n", ( array_search( 'current-menu-item', $item->classes ) || array_search( 'current-page-parent', $item->classes ) ) ? '' : '', $item->url, $item->title );
}
elseif ($depth==2){
$output .= sprintf( "n<li class='secondnavitem %s'>
<div class='pictext'> \I want the featured image here
<a href='%s' class="secondnavtext">%s</a>n", ( array_search( 'current-menu-item', $item->classes) ) ? '' : '', $item->url, $item->title );
}
else {
$output .= sprintf( "n<li class='topnavitem %s'><a href='%s' class="topnavtext">%s</a>n", ( array_search( 'current-menu-item', $item->classes) ) ? '' : '', $item->url, $item->title );
}
}
function start_lvl( &$output, $depth ) {
$indent = str_repeat( "t", $depth );
if ($depth == 0) {
$output .= "n$indent<ul class="nav-level-one" role="submenu">n";
}
if ($depth == 1) {
$output .= "n$indent<ul class="nav-level-two" role="subsubmenu">n";
}
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I’m not sure if I missed something that’s more complex in your question, but sprintf should not be an issue with an img tag. If you want to specify the value as string you can use the %s handler.
sprintf('<div>...<img src="%s" />...</div>', $thumbnail);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0