CMS Development

roman-nich
Membro

How to get count of comments for a specific post?

resolver

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 Avaliação positiva
1 Solução aceita
JasonRosa
Solução
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

resolver

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. 

Exibir solução no post original

0 Avaliação positiva
6 Respostas 6
kmccaskey
Participante

How to get count of comments for a specific post?

resolver

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

0 Avaliação positiva
JasonRosa
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

resolver

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 Avaliação positiva
roman-nich
Membro

How to get count of comments for a specific post?

resolver

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 Avaliação positiva
JasonRosa
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

resolver

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 Avaliação positiva
JasonRosa
Solução
HubSpot Employee
HubSpot Employee

How to get count of comments for a specific post?

resolver

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 Avaliação positiva
Olli
Membro

How to get count of comments for a specific post?

resolver

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 Avaliação positiva