Forum

Thread tagged as: Question, Addons, Comments

Comments app - get comment count per post

I'm using the comments app to add comments to non-blog posts on a site.

I'm using a list-page and detail-page structure and each of the 'details' can have comments.

Is there any easy way of getting and showing the comment count for each post in a list page?

The only solution I've found is to use skip-template on the posts and then iterate through each one checking for comments - which seems cumbersome.

Thanks!

Alex Magill

Alex Magill 0 points

  • 3 years ago
perch_comments_count($parentID, $return=false)

Documents: https://docs.grabaperch.com/functions/comments/perch-comments-count/

Thanks Robert, what I'm looking to do is access the comment count for a post within that post's template - something I'm not sure is possible.

The solution I've found is to use perch_comments_count() to grab the number then pass it in to perch_content_custom as a variable. Though this gets a little messy for long, paginated lists of posts.

Drew McLellan

Drew McLellan 2638 points
Perch Support

For a listing, use the each callback option

perch_content_custom('Posts', [
    'each' => function($item) {
         $item['comment_count'] = perch_comments_count($item['whatever_your_id_is'], true);
         return $item;
    },
    ...
]);

Thanks Drew - that's what I was looking for.

One last question: Is is possible to then access and display the $item['content_count'] value within a template specified by perch_content_custom?

Ideally I'm after adding something like <perch:content id="content_count"> within the template itself.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's how you'd access it (with your tag syntax corrected). You should see the value with <perch:showall />

Thanks Drew - it was the syntax issue that had me stumped (D'oh!)