CMS Development

roman-nich
Membre

How to get count of comments for a specific post?

Résolue

I need to get the count of comments for specific post. I tried to use these functions:

blog_post_by_id(id)
content_by_id(id)


But both functions return empty list of comments and zero comments count. Is there exist any possibility to get count of the comments? 

0 Votes
1 Solution acceptée
JasonRosa
Solution
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

Résolue

Just as an update here @roman-nich's code was perfectly valid but there was an issue on our end that was returning comment_count as 0 even if a post had comments. 

 

{% set post = content_by_id(YOUR BLOG POST ID) %}
{{ post.comment_count }}
{{ post.comment_list }}

For example in the code above, if there was a comment on a post, the comment_count would return 0 and the comment_list would be an empty array. This has now been fixed up so that comment_count should return your comment number and your comment_list should return an array of the comments on the post. 

Voir la solution dans l'envoi d'origine

0 Votes
6 Réponses
kmccaskey
Participant

How to get count of comments for a specific post?

Résolue

Curious where to place these search functions on the backend of Hubspot that will return posts with the most comments. 

0 Votes
JasonRosa
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

Résolue

Hey @roman-nich you should be able to accomplish this by doing something like this: 

{% set comments_number =  content.comment_list|length %}
{{ comments_number }}

The first line would set a HubL variable which getst the number of comments using the length HubL filter. The second line would be the printed number. 

0 Votes
roman-nich
Membre

How to get count of comments for a specific post?

Résolue

I tried this one, and it returned me a zero. I thought that it might be issue with approving of these comments, but all comments approved and the count display correctly on the single page itself.

0 Votes
JasonRosa
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

Résolue

Hey @roman-nich do you have an example blog post that you could link me to where you tried adding that snippet in so that I can take a closer look? Feel free to send me a direct message if that is easier. 

0 Votes
JasonRosa
Solution
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

Résolue

Just as an update here @roman-nich's code was perfectly valid but there was an issue on our end that was returning comment_count as 0 even if a post had comments. 

 

{% set post = content_by_id(YOUR BLOG POST ID) %}
{{ post.comment_count }}
{{ post.comment_list }}

For example in the code above, if there was a comment on a post, the comment_count would return 0 and the comment_list would be an empty array. This has now been fixed up so that comment_count should return your comment number and your comment_list should return an array of the comments on the post. 

0 Votes
Olli
Membre

How to get count of comments for a specific post?

Résolue

Sorry for hijacking this topic, but this issue seems to be related to my problem. We're creating a custom blog post template, which has related blog posts displayed under the content. The related posts are displayed using the tag related_blog_posts. Otherwise it works fine, but just like in the previous example, post.comment_count is always zero and post.comment_list is an empty array.

 

I guess I can use content_by_id() if this is by design.

0 Votes