0

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:

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

<ul>
<?php foreach($recent_posts as $post) : ?>
<li>
    <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
</li>
<?php endforeach; ?>
</ul>   

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

1 Answer 1

0

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.