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 = "";

if( have_posts() ) : 

    while( have_posts() ) :

        $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>";




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.

  • Your second code sample - using WP_Query - is the way to go. Can you show that version? Fixing the syntax error should be straightforward. Using query_posts (in your first sample) is a bad idea.
    – vancoder
    Mar 28, 2013 at 23:54
  • The problem with your query_posts is probably that you should be using 'cat', not 'category'. But again - don't use query_posts.
    – vancoder
    Mar 28, 2013 at 23:56
  • did you also correct lines 6, 7, and 8?
    – Michael
    Mar 29, 2013 at 10:00
  • No, i'm a novice where PHP is concerned and wouldn't even know what needs fixing. Mar 29, 2013 at 15:56

2 Answers 2


Here is the correct way to run this. I am using 'category_name' in theme code lately as it is much easier to read and remember. It uses the category slug. Here I am retrieving the last 5 posts in "uncategorized".

function Last5posts()   {
    $args = array( 'posts_per_page' => 5, 'category_name' => 'uncategorized');                  
    $last_5_posts_query = new WP_Query( $args );
    while($last_5_posts_query->have_posts()) : 
        $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>';
        $content .= '<p class="excerpt">' .get_the_excerpt(). '</p>';
        $content .= '</div>';

return $content;

add_shortcode('Last5Posts', 'Last5posts' );
  • 1
    Whilst this seems to work for the uncategorized category, it also returns the comments section of the posts in question. Any idea why that could be? Mar 29, 2013 at 15:59
  • I changed "category_name" for "cat" and now it doesn't show the comments, but shows the "Leave a reply" box. Mar 29, 2013 at 16:01
  • This code does not have anything to do with the post comment settings. If it is working for you could you please accept the answer so others will know. Thanks. Mar 29, 2013 at 16:14
  • "Allow comments" is off on the page where i posted this shortcode, so there's no other reason as to why this "Leave a reply" would be showing. When i remove the shortcode, the Leave a reply box disappears. Mar 29, 2013 at 16:17
  • There are several reasons this could be happening. Depending on the Theme or plugins you are using, something could be hooking the_excerpt();. Other than that I don't know what else to say. Is it ony 1 instance of "Leave A Reply"? If you can send a link I can take a quick look. Mar 29, 2013 at 21:46

Just simply replace the $args with this one.

$args = array('posts_per_page' => 5, 'post_type' => 'post', 'post_status' => 'publish','orderby'=>'ASC' );

And this show you last 5 posts.

  • I want to display the posts depending on a specific category. Mar 29, 2013 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.