Reputation: 7
I'm trying to show the 3 latest posts of a custom post in a Wordpress shortcode and for so far I'm able to make an shortcode but for some reason the image gets out of the loop which shows the post. Also the html output is different than I have made in my shortcode. The images gets on top of the div which is called "drieberichten" while it should be inside of it.
My Function:
function latest_news_home()
{
global $post;
$html = "";
$my_query = new WP_Query( array(
'post_type' => 'news',
'posts_per_page' => 3
));
if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();
$html .= "<div class='drieberichten'><a href=\"" . get_permalink() . "\" class=\"linkhome\">";
$html .= "<h4>" . get_the_title() . " </h4>";
$html .= "<p class='afbeeldingnieuws'>" . the_post_thumbnail('full') . " </p>";
$html .= "<p class='shortnieuws'>" . excerpt(15) . "</p>";
$html .= "<a href=\"" . get_permalink() . "\" class=\"meerlezen\">Meer lezen...</a></div></a>";
endwhile; endif;
return $html;
}
add_shortcode( 'latest_news', 'latest_news_home' );
The html output:
<img width="600" height="400" src="http://imgurl.nl/post3.jpg" class="attachment-full size-full wp-post-image" alt="" srcset="http://imgurl.nl/post3.jpg 600w, http://http://imgurl.nl/post3-300x200.jpg 300w" sizes="(max-width: 600px) 100vw, 600px">
<div class="drieberichten"><a href="http://websiteurl.nl" class="linkhome"><h4>First News! </h4><p class="afbeeldingnieuws"> </p><p class="shortnieuws">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus aliquam eros ac velit dignissim,...</p></a><a href="http://websiteurl.nl" class="meerlezen">Meer lezen...</a></div>
Does anyone have a suggestion? I have tried to make a new thumbnail size, I tought it has something to do with that but is does not solve the problem.
Upvotes: 0
Views: 314
Reputation:
Replace
the_post_thumbnail('full')
with
get_the_post_thumbnail( null, 'full', '' )
The problem is the_post_thumbnail() echos the result. You need a function that returns a string
Upvotes: 0