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

Overview of Core Features: Learn about View Count and Comment Number

In AnQiCMS, each article is accompanied by some metadata, including the article's view count (Views) and comment count (CommentCount).These data can directly reflect the popularity of the article and the level of community activity.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 scenarios such as article lists and recommended related articles 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 as)article/detail.htmlorproduct/detail.html) when showing the number of views and comments,archiveDetailTags are your preferred tool. This tag is specifically used to retrieve detailed data of the current or specified article.

UsearchiveDetailThe tag 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 directly access it through{{archive.Views}}and{{archive.CommentCount}}such variables.

Another more general way is to usearchiveDetaillabel combined withnameparameters to get the value of specific fields. For example, to display the page views of an article, you can write it like this:

<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 subsequent processing or combined display,archiveDetailSpecify a variable name for the tag:

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

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

Display page views and comment counts in the article list

It is also important to display the view count and comment number of each article in the article list (such as the home page recommendation, category list, tag list, or search results page), in addition to the detail page. 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 body.item.Viewsanditem.CommentCount.

Here is an example of displaying the number of views and comments in the 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,{% archiveList articles ... %}Label obtained a set of article data and,{% for item in articles %}looped through each article.{{ item.Views }}and{{ item.CommentCount }}Extracted and displayed the view count and comment count of each article. At the same time,stampToDateTag to format the article's publish time, making the information more complete and readable.

Actual application scenarios and pairing usage

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

  • Enhance user experience:Visitors can quickly see which content is more popular and help them quickly filter the articles they are interested in.
  • Content popularity feedback:For content creators, this data is valuable feedback that helps them understand which types of content are more popular with 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 visually appealing. For example, you can 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.

Common Questions (FAQ)

  1. Why doesn't the number of views and comments on my article show?

    • Check the spelling of the tags:Make sure you are using in the template.ViewsandCommentCountField name is spelled correctly and consistent in case. AnQiCMS template tags are case-sensitive.
    • Does the data exist:The newly released article may not have any views or comments yet; these fields will display as 0. If so, this is normal.
    • Template context:EnsurearchiveDetailorforIn the loopitemVariables correctly point to the article data. For example, using it directly on a non-article detail page.{{archive.Views}}May not be able to retrieve data.
    • Cache issue:If you have just updated an article or added comments but the front end does not display immediately, try clearing the cache of AnQiCMS backend or refreshing the browser cache.
  2. Is the page view of the article updated in real time? Can I manually modify the page views or comment numbers?

    • The article view count is usually automatically accumulated 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 using the article editing feature of the AnQiCMS backend, or in extreme cases, directly manipulating the database (not recommended for non-professionals to attempt).
  3. How to style the displayed browsing volume and comment count?

    • The numbers displayed themselves are plain text, you can style the 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.Combine icon fonts (such as Font Awesome) or SVG icons to make the display more professional and attractive.