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 );
$content = "";
query_posts($args);
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.
you can use code just like below
using this wordpress by default will use last 5 post after this code…
Use this
$catnames[1] denotes which category u want to use related to that post.
Check this out: query posts parameters; you should definitely using “cat” instead of category.
Also, are you ending your “while” with an “endwhile;”?
What does your complete code now look like?