How to display the number of views and comments on articles in AnQiCMS templates?

Calendar 👁️ 60

In website operation, the number of page views and comments on articles is an important indicator of the popularity of content and user engagement.AnQiCMS provides flexible template tags, allowing you to easily display these key data on the website front-end, thus better attracting visitors and enhancing content value.

Overview of core features: Understand the number of views and comments

In AnQiCMS, every article is accompanied by some metadata, including the number of views (Views) and the number of comments (CommentCount).This data can directly reflect the popularity of the article and the activity of the community.Generally, you would want to display this information on the article detail page, allowing readers to understand how many people have read this article and how many discussions have been sparked around it.In addition, displaying this data in article lists, related article recommendations, and other scenarios can also help visitors quickly judge the value and attractiveness of the content.

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

When you need to display the number of views and comments on a specific article page (such asarticle/detail.htmlorproduct/detail.html), display the number of views and comments.archiveDetailTags are your preferred tool. This tag is specifically used to obtain detailed data for the current or specified article.

UsearchiveDetailThe label is very intuitive. If you are on the article detail page and the template context already contains the data of the current article, you can access it directly through{{archive.Views}}and{{archive.CommentCount}}such variables.

Another more general way is to usearchiveDetailLabel collaborationnameparameters to get the value of a specific field. For example, to display the page views of an article, you can write:

<div>阅读量:{% archiveDetail with name="Views" %}</div>

Similarly, to display the number of comments on an article, you can do this:

<div>评论数:{% archiveDetail with name="CommentCount" %}</div>

If you want to assign these data to a variable for further processing or combination display, you canarchiveDetailLabel a variable name:

{% archiveDetail articleViews with name="Views" %}
{% archiveDetail articleComments with name="CommentCount" %}
<p>这篇文章已被阅读 {{ articleViews }} 次,共有 {{ articleComments }} 条评论。</p>

This code will retrieve the number of views and comments from the currently loaded article and clearly display them on the page.

Display views and comment count in article list

In addition to the detail page, it is also important to display the number of views and comments for each article in the article list (such as home page recommendations, category lists, tag lists, or search result pages). AnQiCMS'sarchiveListTags can help you easily achieve this.

When you usearchiveListWhen tags display multiple articles in a loop, the data of each article will be assigned to the loop.itemVariable (You can customize this variable name). You can directly access it within the loop.item.Viewsanditem.CommentCount.

Here is an example of displaying the number of views and comments in an article list:

{% archiveList articles with type="page" limit="10" %}
    {% for item in articles %}
    <article>
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p>{{ item.Description }}</p>
        <footer>
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
            <span>浏览:{{ item.Views }} 次</span>
            <span>评论:{{ item.CommentCount }} 条</span>
        </footer>
    </article>
    {% empty %}
    <p>当前没有可用的文章。</p>
    {% endfor %}
{% endarchiveList %}

In this code block,{% archiveList articles ... %}The tag obtained a set of article data and processed through{% for item in articles %}Looped through each article. Inside the loop,{{ item.Views }}and{{ item.CommentCount }}Extracted and displayed the views and comments of each article. At the same time, it also matched withstampToDateTags to format the publishing time of the article, making the information more complete and easy to read.

Actual application scenarios and usage

Combine the number of page views and comments with other information about the article (such as publication date, author, category, etc.) to greatly enrich the page content and provide visitors with more useful references.

  • Enhance user experience:Visitors can easily see which content is more popular, helping them quickly filter articles of interest.
  • Content popularity feedback: For content creators, this data is valuable feedback that helps them understand which types of content are more favored by readers.
  • Social proof: High number of views and comments can serve as a form of "social proof," encouraging new visitors to read and participate in discussions.

In your template, you can add icons, different colors, or font styles to these numbers to make them more prominent and beautiful. For example, add an eye icon before the number of views.👁️Add a bubble icon before the number of comments💬All of these can make the data more vivid

Frequently Asked Questions (FAQ)

  1. Why don't my article views and comment numbers show?

    • Check the spelling of tags:Make sure you are using in the templateViewsandCommentCountThe field name is spelled correctly and the case is consistent. The template tags of AnQiCMS are case-sensitive.
    • Does the data exist:The newly published article may not have any views or comments yet, these fields will display as 0. If this is the case, it is normal.
    • Template context:EnsurearchiveDetailtags orforin the loopitemThe variable correctly points to the article data. For example, it is used directly on a non-article detail page{{archive.Views}}Data may not be able to be retrieved.
    • Cache problem:If you have just updated an article or added comments, but the front page does not display immediately, try clearing the AnQiCMS backend cache or refreshing the browser cache.
  2. Is the number of page views of the article updated in real time? Can I manually modify the number of page views or comments?

    • The article view count is usually automatically incremented each time a user visits the detail page, not queried in real-time from the database, and usually has a counting mechanism.AnQiCMS will automatically handle the statistics of page views.
    • The number of comments is calculated based on the actual comments received.
    • In most cases, front-end template tags do not support direct manual modification of these counts.If you indeed need to adjust, this usually requires going through the AnQiCMS article editing feature, or in extreme cases, directly manipulating the database (not recommended for non-professionals to attempt).
  3. How to style the displayed page views and comment counts?

    • The numbers displayed themselves are plain text, you can wrap the numbers in HTML elements such as<span>or<div>Add CSS styles to achieve beautification.
    • For example, you can set font size, color, bold, or add background color, border, etc.Combining icon fonts (such as Font Awesome) or SVG icons can make the display more professional and attractive.

Related articles

How to automatically parse the URL string in the text of AnQiCMS to make it clickable?

In daily content operation, we often need to embed various external or internal links in text content such as articles, product descriptions, and even category summaries.However, manually converting each plain text URL string into a clickable HTML link is not only time-consuming and labor-intensive, but also prone to link errors due to negligence, affecting user experience and search engine optimization.

2025-11-07

How to display the category thumbnail and name in the article list?

## SecureCMS: Display category thumbnails and names elegantly in article lists In website operation, the article list page is an important entry point for users to browse content and discover information.A beautifully designed, informative list of articles can not only enhance user experience but also effectively guide users to delve deeper into reading.The traditional article list may only display titles, publication time, summaries, and other information, making it somewhat monotonous.

2025-11-07

How to convert a timestamp to a specified date format and display it in the AnQiCMS template?

In Anqi CMS template design, we often encounter the need to display timestamp data in a human-readable date format.Whether it is the publication time of the article, the update date of the content, or the record of user activities, this data is usually stored in the database in the form of timestamps.It is particularly important to master how to convert timestamps to the specified date format in templates so that website visitors can clearly and intuitively understand this information.

2025-11-07

How to ensure that custom parameters set in the AnQiCMS backend can be displayed in the front-end template?

The AnQi CMS endows content management with high flexibility, among which the customizable parameter function is the key to meeting the needs of personalized content display.How to accurately display the unique parameters set for content models such as articles, products, or categories on the website front-end template is a concern for many users.This article will detail this process, helping you fully utilize the powerful customization capabilities of Anqi CMS.

2025-11-07

How to customize the display of image carousel (Banner) through template tags in AnQiCMS?

AnQiCMS as an efficient and customizable enterprise-level content management system has a significant advantage in the flexibility of content display.For the common picture carousel (Banner) feature on the website, AnQiCMS provides various ways to customize the display through template tags, allowing you to easily achieve diverse Banner display effects according to different page and business needs.

2025-11-07

How to safely output HTML code in AnQiCMS templates without escaping?

When building and managing website content, we often need to display rich text content with specific formats or interactive effects on the page, such as the main body of articles, product descriptions, and even embedded video players or maps.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that, when handling these requirements, defaults to taking an important security measure: escaping the HTML code output in the template.

2025-11-07

How to display user details and user grouping information in the AnQiCMS user group management?

In AnQiCMS, effectively managing users and user groups is a key factor in building personalized websites, implementing membership strategies, and even achieving content monetization.By flexibly using its built-in template tags, we can easily display users' detailed information and their user group information on the website front end, providing a more customized and interactive experience.The "User Group Management and VIP System" feature provided by AnQiCMS allows website operators to divide different user groups according to business needs and define exclusive permission levels for these groups.

2025-11-07

What is the purpose of the `addslashes` filter in AnQiCMS templates?

During the AnQiCMS template development and content operation process, we often encounter situations where we need to display dynamic content on the web page.This content may come from a database, be entered by a user, or generated by the system.Most of the time, the AnQiCMS template engine automatically escapes output variables for security reasons, converting `<` to `&lt;This effectively prevents common cross-site scripting (XSS) attacks by converting `,`"` to `&quot;` and so on.However, in certain specific scenarios, simple HTML

2025-11-07