How to display the number of comments for each article on the article list page?

Calendar 👁️ 73

In website operation, the article list page is not only the entrance to the content, but also a window for users to understand the activity level of the website.When users see that every article is accompanied by the number of comments, it not only effectively enhances the interactivity and attractiveness of the article, but also brings positive social proof to the website, encouraging more readers to participate in discussions.

For users of AnQiCMS, it is a very direct and practical feature to display the number of comments for each article on the article list page.AnQiCMS provides flexible and powerful template tags, allowing you to easily integrate dynamic data into the website front end without complex secondary development.

Locate and edit your article list template

To implement the display of comment counts on the article list page, you need to find the template file responsible for rendering the article list. Usually, this may be your homepage template (index/index.html),Article category list page template(for example)article/list.htmlOr more specifically,article/list-{分类ID}.html),Even search results page templates(search/index.htmlIf you are not sure which file it is, you can refer to the AnQi CMS template design agreement document to understand the template path for different page types.

After finding the corresponding template file, you will find that it usesarchiveListThe tag is used to loop and display article content. This tag is the core content list call tag of AnQiCMS, which can obtain various detailed information of the article.

The acquisition of core tags and comment counts

InarchiveListWithin the loop of tags, you canitem.CommentCountAccess the comment count corresponding to each article. HereitemRepresents each article object in the current loop.

Let's look at a basic code structure example, assuming you are displaying articles on an article list page:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article class="article-item">
        <h2 class="article-title"><a href="{{item.Link}}">{{item.Title}}</a></h2>
        <div class="article-meta">
            <span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{{item.Views}}</span>
            {# 这里是显示评论数量的关键部分 #}
            <span class="article-comments">
                评论:{{item.CommentCount}}
            </span>
        </div>
        <div class="article-description">
            <p>{{item.Description}}</p>
        </div>
    </article>
    {% empty %}
        <p>当前分类下暂无文章。</p>
    {% endfor %}
{% endarchiveList %}

In the above code snippet,{{item.CommentCount}}It will directly output the total number of comments for the article.

Optimize display effect: Tips and click jump when no comments

Directly displaying numbers is clear, but in practical applications, we may want to optimize the display of comment counts, for example:

  1. Friendly prompt when there are no comments:If an article does not have comments temporarily, directly displaying "0" may not be friendly. You can set a more natural prompt, such as "No comments yet."
  2. Click to jump to the comment section:Allow users to click on the comment count and directly jump to the comment area of the article detail page, enhancing the user experience.

Combining these optimizations, we can further improve thearticle-commentscode section:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article class="article-item">
        <h2 class="article-title"><a href="{{item.Link}}">{{item.Title}}</a></h2>
        <div class="article-meta">
            <span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{{item.Views}}</span>
            <span class="article-comments">
                评论:
                <a href="{{item.Link}}#comments"> {# 添加 #comments 锚点,假设评论区id为comments #}
                    {% if item.CommentCount > 0 %}
                        {{item.CommentCount}} 条评论
                    {% else %}
                        暂无评论
                    {% endif %}
                </a>
            </span>
        </div>
        <div class="article-description">
            <p>{{item.Description}}</p>
        </div>
    </article>
    {% empty %}
        <p>当前分类下暂无文章。</p>
    {% endfor %}
{% endarchiveList %}

This code uses{% if item.CommentCount > 0 %}make conditional judgments and add a link to the number of comments.{{item.Link}}#commentsmeans that clicking will navigate to the detail page of the article and try to jump to the page ID ofcommentsThe element position, which is usually the start of the comment area. Of course, you also need to ensure that your article detail page has a comment area corresponding toid="comments"the element.

Background configuration and precautions

To ensure that the comment feature works properly and displays the number, you need to make sure of the following:

  1. The comment feature has been enabled:Please check if the 'Content Comment Management' is enabled in the Function Management of the Anqi CMS backend.
  2. The comment content has been reviewed:Comments that have been reviewed and published (status is displayed) will be counted. The background content comment management page can review comments.

By following these simple steps, you can clearly display the number of comments for each article on the article list page of AnQi CMS.This minor change will significantly enhance the interactivity and user engagement of your website, making your content more vibrant.


Frequently Asked Questions (FAQ)

1. I followed the steps, but the comment count always shows as 0, why is that?

First, please check if the comment function has been enabled on your website backend, and whether the article indeed has comments.If there are comments, please confirm whether these comments have passed the review and are in the "display" status. Comments that have not been reviewed or marked as hidden will not be counted.In addition, try to clear the website cache (there is a “Update Cache” function in the background) and the browser cache to ensure that the template changes and data are the most up-to-date.

2. Besides the comment count, what other article data can I display on the article list page?

Of Security CMSarchiveListTags provide rich article data fields. In addition,CommentCountyou can easily display the article title (item.Title) and article link (item.Link) publication time (item.CreatedTime), Page views (item.Views), Description (item.Description) as well as the thumbnail (item.Thumboritem.Logo) etc. You can flexibly choose and combine these fields to enrich the display information of the article list.

How do I add an icon next to the number of comments?

Adding an icon to the template is very simple. You can{{item.CommentCount}}Insert an icon label at the beginning or end, for example, using Font Awesome or another icon library. For example:评论:<i class="fa fa-comments"></i> <a href="{{item.Link}}#comments">...</a>Or, if the icon is an image:评论:<img src="/static/images/comment-icon.png" alt="评论" /> <a href="{{item.Link}}#comments">...</a>Remember to include the corresponding CSS or JS file to ensure the icons display correctly.

Related articles

How to ensure that the website displays a custom prompt to users when it is in "shutdown" status?

In website operation, we sometimes encounter situations where we need to temporarily close the website, such as for system maintenance, major upgrades, data migration, or dealing with unexpected security events.For any reason, directly allowing users to access a blank page or an error page will severely affect user experience and even damage the brand image.At this time, it is particularly important to clearly display a friendly prompt message to visitors.

2025-11-07

How to call and display the independent domain of the website backend configured in the template?

In the flexible content management system of AnQiCMS, site operators often need to display some key background configuration information in the website template according to business needs.Among the settings such as 'Background Independent Domain', it not only concerns the security of the system, but may also need to be displayed on the website front-end in certain scenarios, such as being used as a quick access entry for internal personnel, or for information explanation on specific pages. Today, let's delve into how to cleverly call and display the independent domain you have configured in the AnQiCMS template.###

2025-11-07

How to display the AnQiCMS built-in system language package switch feature on the front page?

AnQi CMS provides a flexible mechanism to allow your website to better serve users in different languages around the world.When you want to implement language switching on the front page, the tags and backend configuration built into AnQiCMS can well meet this need.Understanding AnQiCMS language package mechanism First, we need to clarify the concept of "language package" in AnQiCMS.The system language package built into AnQiCMS is mainly responsible for switching the website backend interface and the language of the**system built-in prompts, labels, etc.**on the front-end page.

2025-11-07

How to display the unique content and images of a custom page (single page)?

In website operation, we often need to create some unique content and personalized page layouts, such as 'About Us', 'Contact Information', or a specific event detail page.AnQiCMS (AnQiCMS) has provided us with a powerful custom page (single page) feature, which not only allows us to easily manage the text content of these pages, but also flexibly display their exclusive images and customized layouts.How to specifically operate, so that the custom page has its own unique content and image display method?

2025-11-07

How to use the `pagination` tag to build standard pagination navigation in the template?

When the content of a website becomes richer, a clear and efficient pagination navigation system is crucial for improving user experience.The AnQiCMS template system provides a powerful `pagination` tag that allows us to easily build standard, beautiful pagination navigation.Next, we will delve deeper into how to use this tag in templates.

2025-11-07

How to display the custom parameter list in the article content on the front page?

AnQiCMS as an efficient and customizable content management system provides powerful content model features, allowing us to add various custom fields to articles according to different business needs.These custom parameters not only make the article content more rich and structured, but also allow for personalized display on the front page, greatly enhancing the flexibility and user experience of the website.Today, let's delve into how to flexibly display these custom parameter lists in the AnQiCMS front page.

2025-11-07

How to truncate the title or abstract of an article and display it with an ellipsis?

In website content operation, titles and summaries are important elements to attract visitors to click and learn more details.Especially on list pages, recommendation positions, or the homepage, the limited space requires us to refine and cut these contents.To maintain the neatness and aesthetics of the page while fully conveying the core information, it is a common optimization strategy to truncate overly long article titles or summaries and end them with an ellipsis "..."}AnQiCMS (AnQi CMS) provided us with flexible and powerful tools to meet this requirement.

2025-11-07

How to use logical tags like `if` and `for` in templates to control conditional and repeated display of content?

In website content management, displaying dynamic and diverse content is the key to improving user experience and website attractiveness.AnQiCMS (AnQiCMS) provides a powerful template system, drawing inspiration from the Django template engine syntax, allowing you to easily control the display of content through logical tags.Among them, the `if` tag is used for conditional judgment, while the `for` tag is used for loop iteration, they are the foundation for building flexible and variable web pages.

2025-11-07