I have created a shortcode in my functions.php which is designed to return the 3 latest blog posts (titles, thumbnail, read more link) with a tag of “highlight”. The PHP in my shortcode successfully recognises the tag parameter but it only returns one post.
As you can see, I am using the “posts_per_page” parameter to define the number of posts I want to return. At the moment, when I enter 1, 2 or 3 it simple returns the 1st, 2nd or 3rd post in date order – rather than the 3 latest posts. I have unsuccessfully tried “numberposts” and “showposts” (trial and error!).
Can anyone please tell me what I’m missing here? This is in my functions.php file:
function highlight_posts() {
$args = array('tag' => 'highlight' , 'posts_per_page' => 4);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$return_string = '<div class="blog-post-highlight"><div class="blog-post-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($page->ID, array( 300, 168 )).'</a></div><div class="blog-post-content"><h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3><p class="read-more"><a href="'.get_permalink().'">Read More...</a></p></div><div class="clear"></div></div>';
endwhile;
wp_reset_postdata();
return $return_string;
}
add_shortcode( 'highlightposts', 'highlight_posts' );
And this is deployed within the WYSIWYG editor by inserting [highlightposts]
try this:
You were giving new values to the $return_string, instead of adding the new ones.
Check if it was that