I have installed a plugin that lets me put a PHP code into the widget - basically, what I want to do is to create a "recent posts" area, where you can see a cover of the post (featured image's thumbnail), title and number of comments for them.

So far I managed to create a code that works - minus the comments. I looked at few different things, like get_comments_number, comments_number, wp_count_comments - but so far I can't get this to work.

Here's current code:

$recent_posts = wp_get_recent_posts(array('numberposts' => 3));

<?php foreach($recent_posts as $post) : ?>
    <a href="<?php echo get_permalink($post['ID']) ?>">
    <?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
    <?php echo $post['post_title'] ?>
    </a> -  //here I want to show the number of comments
<?php endforeach; ?>

Edit: did it myself :D somehow my PHP stopped being saved and after a couple of more tries I managed to find a way. Added this and it seems to work:

<?php echo get_comments_number($post['ID']); ?>
  • Did you get an error using wp_count_comments? What parameter did you pass to it?
    – Dhaulagiri
    Dec 12, 2013 at 2:17
  • Hey, just made an edit - managed to find a solution to my problem. But thanks for replying!
    – user2143561
    Dec 12, 2013 at 2:20
  • That is precisely what I was going to post as my answer :)
    – Dhaulagiri
    Dec 12, 2013 at 2:20

1 Answer 1


Try this

<?php echo get_comments_number( $post['ID'] ); ?> 

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.