How many comments are displayed for each article on the article list page?

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 the number of comments attached to each article, 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 who use AnQiCMS, displaying the comment count for each article on the article list page is a very direct and practical feature.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)、English category list page template (for example)article/list.html,or more specifically)article/list-{分类ID}.html),or even the search results page templatesearch/index.html)。If you are unsure about the specific file, you can refer to the template design agreement document of Anqi CMS to understand the template paths corresponding to different page types.

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

Core tag and comment count retrieval

InarchiveListInside the loop of tags, you can useitem.CommentCountto access the number of comments corresponding to each article. Hereitemrepresents the article object in the current loop.

Let's take a 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: Prompt and jump when there are 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 the article does not have any comments temporarily, 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 area:Let users jump directly to the comment area of the article detail page when they click on the number of comments, enhancing the user experience.

Combining these optimizations, we can further improve:article-commentsThe code in part:

{% 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 %}Perform conditional judgment and add a link for the number of comments.{{item.Link}}#comments意味着点击后会导航到该文章的详情页,并尝试跳转到页面中 ID 为EnglishcommentsThe element position, which is usually the start of the comment area. Of course, you also need to make sure that the comment area of your article detail page has a correspondingid="comments"element.

Background configuration and precautions

To ensure that the comment feature operates normally and displays the count, you also need to make sure of the following:

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

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


Common Questions and Answers (FAQ)

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

First, please check if the comment feature is enabled on your website backend and if the article indeed has comments.If comments exist, please confirm whether these comments have been approved and are in the "Visible" status. Comments that are not approved 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 latest.

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

Anqi CMS'sarchiveListTags provide rich article data fields. In addition,CommentCountyou can also easily display the article title (item.Title), article link (item.Link), publishing time (item.CreatedTime) views (item.Views) and the description (item.Description)、thumbnail (item.Thumboritem.Logo) etc. You can flexibly select and combine these fields according to the design needs of the template to enrich the display information of the article list.

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

Adding an icon to the template is very simple. You can in the{{item.CommentCount}}Insert an icon label at the beginning or end, for example, using Font Awesome or other icon libraries. 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 icon displays correctly.