How to display the reading and comment counts of articles on the front end?

Calendar 👁️ 77

In website operation, the number of article views and comments is an important indicator of the popularity of content and the attraction of user interaction.AnQiCMS as a flexible content management system provides a convenient way to display these data on the website front-end.By calling simple template tags, even without delving into programming, these functions can be easily implemented.

The AnQiCMS template system borrows the syntax of the Django template engine, using double curly braces{{变量}}To output variable content, use single curly braces and percentages{% 标签 %}Call the feature tag. Understanding these basic rules can help you customize the display effect of the website more efficiently.

Display the number of reads and comments on the article list page.

When we need to display the reading volume and comment count of each article in the article list (such as article recommendations on the homepage, categorized article lists, etc.), we can usearchiveListTag to get article data and display each one by looping.

archiveListThe tag can retrieve a collection of articles under specified conditions. While traversing this collection, the data of each article will be represented by a variable (usuallyitem), and we can directly access ititemThe property of the object to get the reading volume and comment quantity.

Here is an example of code that displays a list of articles and their corresponding reading volume and comment quantity.

{# 假设我们正在获取文章模型ID为1,显示数量为10的文章列表 #}
{% archiveList articles with moduleId="1" type="list" limit="10" %}
    {% for article in articles %}
    <div class="article-card">
        <h3><a href="{{article.Link}}">{{article.Title}}</a></h3>
        <p class="article-meta">
            <span>发布日期:{{stampToDate(article.CreatedTime, "2006-01-02")}}</span>
            <span>阅读量:{{article.Views}}</span>
            <span>评论数:{{article.CommentCount}}</span>
        </p>
        <p>{{article.Description}}</p>
        <a href="{{article.Link}}" class="read-more">查看详情</a>
    </div>
    {% empty %}
    <p>暂时没有文章可供显示。</p>
    {% endfor %}
{% endarchiveList %}

In the code above:

  • {% archiveList articles ... %}Called the article list tag and assigned the obtained article collection toarticlesVariable.
  • {% for article in articles %}Looped through each article.
  • {{article.Views}}Directly output the reading volume of the current article.
  • {{article.CommentCount}}The current number of comments of the article is output.

In this way, you can dynamically display the reading and comment count of each article in any article list area of the website.

Display the number of reads and comments on the article detail page

When a user clicks to enter a specific article detail page, the detailed data of the article is usually displayed below or at the end of the article title, including its reading volume and total number of comments. In the AnQiCMS article detail page, all the information of the current article is automatically loaded into a namedarchiveThe global variable in. Therefore, we can directly accessarchivethe properties of the object to get the required data.

The following is an example code snippet showing the reading and comment count on an article detail page:

<article class="article-detail">
    <h1 class="article-title">{{archive.Title}}</h1>
    <div class="article-info">
        <span>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</span>
        <span class="views-count">阅读量:{{archive.Views}}</span>
        <span class="comment-count">评论数量:{{archive.CommentCount}}</span>
    </div>
    <div class="article-content">
        {{archive.Content|safe}}
    </div>
    {# 这里可以继续添加评论列表等内容 #}
</article>

In this code block:

  • {{archive.Title}}The article title was output.
  • {{archive.Views}}The reading count of the current article was directly obtained and displayed.
  • {{archive.CommentCount}}Directly retrieved and displayed the number of comments for the current article.

This method is very intuitive and can be easily integrated into the article detail page without any additional tag calls, enabling the integration of these core data.

About the comment list supplement

Although the above mainly discusses displaying the total number of comments, if you need to further display the specific comment content list, AnQiCMS also providescommentListLabel. This tag can be used to get the comments details of a specified article, it is often used with pagination tags.paginationtogether.

For example, you can call it in the comments area of the article detail page.

<div class="comments-section">
    <h3>用户评论 (共 {{archive.CommentCount}} 条)</h3>
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {% for comment in comments %}
        <div class="single-comment">
            <p><strong>{{comment.UserName}}</strong> 于 {{stampToDate(comment.CreatedTime, "2006-01-02 15:04")}} 评论:</p>
            <p>{{comment.Content}}</p>
        </div>
        {% empty %}
        <p>目前还没有评论,快来发表您的看法吧!</p>
        {% endfor %}

        {# 评论分页 #}
        {% pagination pages with show="5" %}
            {# 分页链接的渲染逻辑 #}
        {% endpagination %}
    {% endcommentList %}
</div>

Herearchive.CommentCountUsed to display the total number of comments, whilecommentListThe tag is responsible for fetching and displaying the specific content of each comment in a loop.

Useful Tips

In actual operation, please pay special attention to the case sensitivity of AnQiCMS template tags and field names, such asViewsandviewsIt is different. After modifying the template file and uploading it, if the front-end page does not update immediately, you can try to clear the browser cache or the system cache of AnQiCMS backend to ensure that the new template file takes effect.

AnQiCMS provides these tag calling methods, making it simple and efficient to dynamically display website content, which helps operations personnel better manage and present website content, enhancing user experience and website interactivity.


Frequently Asked Questions (FAQ)

Q1: Why did the reading count or comment count not display on the front-end page after I added the code according to the tutorial?

A1: In this case, you may need to check several aspects. First, make sure that the labels and field names in the template file are spelled correctly, AnQiCMS templates are case-sensitive, for exampleViewsandviewsIt can be recognized as a different field. Secondly, check if the article comment function is enabled in the AnQiCMS background.Sometimes, after updating the template file, the browser or system cache may not refresh in time. You can try clearing the browser cache or manually clearing the system cache in the "Update Cache" function of the AnQiCMS backend.

Q2: How is the article reading volume of AnQiCMS counted? Is there a way to customize the counting logic?

A2: AnQiCMS automatically counts the page views of articles.The system design includes traffic statistics and crawler monitoring functions, and the reading volume data is usually recorded through internal mechanisms each time a user visits the article page.The official documentation does not directly mention the method for customizing the reading statistics logic, which is usually a built-in core feature of the system.If you need more complex statistical analysis, you can consider combining the traffic statistics provided by AnQiCMS for analysis, or through secondary development to achieve specific statistical needs.

Q3: Do I support displaying the reading volume in the format of '1.2k' or '1 thousand+'?

A3: AnQiCMS template tags directly output the original number of article views, for example “1200”.If you need to display in the format "1.2k" or "1 thousand+", the template itself does not provide a built-in formatting filter to achieve this effect.You can consider the following two methods: the first is to combine front-end JavaScript code to format numbers after the page is loaded.Secondly, write some conditional logic in the template ({% if ... %}Labels) to manually concatenate strings based on the size of numbers, but this will make the logic of the template relatively complex.

Related articles

How to use the content model in AnQiCMS to define different content display structures?

AnQiCMS Content Model: The Smart Choice for Creating Flexible and Variable Website Content In the digital age, a website is not just a platform for displaying information, but also a gathering point for diverse content such as corporate brands, product services, and professional knowledge.If always using a fixed display structure for different types of content, the website content is likely to become monotonous and difficult to manage efficiently.AnQiCMS fully understands this, the "Content Model" feature it provides is the core tool to solve this pain point, helping us easily define and manage various unique content display structures.Imagine

2025-11-07

How to render the category Banner image set in the backend in the front-end template?

In AnQi CMS, displaying the Banner images of categories on the front-end template is a common operation that can effectively enhance the visual effects of the page.AnQi CMS provides flexible backend settings and powerful front-end template tags, making this process simple and intuitive. --- ### Step 1: Set up Category Banner Image in the Backend Firstly, we need to upload a Banner image for the target category in the Anqi CMS backend management system.This operation is very intuitive: 1. Log in to your Anqi CMS backend.2.

2025-11-07

How to display a custom shutdown prompt to users when the website is under maintenance in AnQiCMS?

During the operation of the website, maintenance and upgrades are inevitable steps.Whether it is system updates, data migration, or solving some unexpected problems, the website needs to be temporarily closed to ensure the smooth progress of operations and the integrity of the data.How can you inform the visitor in a friendly manner that the website is under maintenance, rather than making them face a blank page or error message, which is particularly important.AnQiCMS provides a flexible shutdown prompt mechanism to help you display professional and clear custom information to users during website maintenance.### Enable Station Mode

2025-11-07

How can I use the custom URL alias of articles or categories for pseudo-static link display?

## Leverage the advantages of AnQi CMS custom URL alias to create SEO-friendly static links In website operations, a clear and semantically meaningful URL address not only helps users understand the content of the page at a glance, but also wins the favor of search engines, thus improving the inclusion and ranking of the website.AnqiCMS (AnqiCMS) is well-versed in this, providing powerful custom URL alias and flexible static configuration features to help website operators easily achieve this goal.Why is it so important to use custom URL aliases and permalinks?Imagine

2025-11-07

How to prevent malicious collection of content in AnQiCMS and affect the exclusivity of front-end content?

Original content is the core value of the website, it condenses our thoughts, time and energy.However, the content has been maliciously collected, plagiarized, and published on other platforms, not only diluting the exclusivity of our content, but also potentially affecting the SEO performance of the website and even damaging the brand image.As website operators, we all hope that our front-end content can maintain its original exclusivity. 幸运的是,AnQiCMS was designed with this pain point in mind from the very beginning, incorporating multiple powerful features to help us effectively prevent content from being maliciously scraped and protect our original value. ### 1. Core Defense

2025-11-07

How to dynamically add a website name suffix to the page title to optimize SEO display?

In content operation, the page title is undoubtedly the first barrier to attracting the attention of search engines and users.A clear, keyword-rich, and brand-consistent title that not only improves user click-through rate but is also a key element of search engine optimization (SEO).AnQiCMS is an enterprise-level content management and SEO optimization system that provides a flexible and powerful mechanism, allowing you to easily add a website name suffix to the page title, thereby achieving a win-win situation for brand enhancement and SEO.### Unified Brand Image

2025-11-07

How to display the name and description of user groups in AnQiCMS template?

In AnQiCMS template, flexibly display user group names and introductions AnQiCMS, as an efficient content management system, provides great convenience for website operators with its "User Group Management and VIP System" function, which can easily achieve user classification, content permission control, and member value-added services. However, in actual operation, how to naturally present these carefully set user group information, such as their names and detailed introductions, in the website front-end template so that users can clearly understand their identity privileges or exclusive rights to various services is a problem that template developers and content operators often encounter.

2025-11-07

How to control the automatic compression and thumbnail display of AnQiCMS front-end images after uploading?

In the process of operating a website, the management and optimization of image content is a key factor in improving user experience, accelerating website loading speed, and even affecting search engine rankings.AnQiCMS fully considered these needs, providing a flexible image processing mechanism that allows us to easily control the automatic compression and thumbnail display of uploaded images on the front end.### Content Setting: The Core Hub of Image Processing To finely manage the images on the AnQiCMS website, we need to first go to the "Background Settings" menu and then click on the "Content Settings" option

2025-11-07