Hola! ¡Tenemos nuestra Comunidad en Español!

How to get count of comments for a specific post?

resolver
Nuevo colaborador

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? 

Responder
0 Me gusta
1 Soluciones aceptada

Soluciones aceptadas
Empleado de HubSpot

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. 

Responder
0 Me gusta
6 Respuestas 6
Highlighted
Empleado de HubSpot

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. 

Responder
0 Me gusta
Nuevo colaborador

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.

Responder
0 Me gusta
Empleado de HubSpot

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. 

Responder
0 Me gusta
Empleado de HubSpot

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. 

Responder
0 Me gusta
Nuevo colaborador

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.

Responder
0 Me gusta
Colaborador ocasional

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

Responder
0 Me gusta