I'm trying to show the last 5 posts from a specific category, which will be linked to a function so i can insert the shortcode in a Wordpress page. The code i have is as below, it does everything i need (although i want to add featured image too) except it does not show posts from a specific category.
I've tried numerous things, but cannot find a working fix.
function Last5posts()
{
$args = array( "showposts" => 5, "category" => 3 );
query_posts($args);
$content = "";
if( have_posts() ) :
while( have_posts() ) :
the_post();
$link = get_permalink();
$title = get_the_title();
$date = get_the_date();
$content .= "<div class='latest-posts'>";
$content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>\n";
$content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
$content .= "</div>";
endwhile;
wp_reset_query();
endif;
return $content;
}
add_shortcode('Last5Posts', 'Last5posts' );
I have tried replacing lines 3 and 4 with the code below, but it throws an error "syntax error, unexpected '}' on line 31".
$catquery = new WP_Query( 'cat=3&posts_per_page=10' );
while($catquery->have_posts()) : $catquery->the_post();
Any help would be greatly appreciated.