How to display the document's view count and comment number?

In website content operation, accurately displaying the document's page views and the number of user comments is a key indicator for measuring the popularity of the content and the level of user activity.AnQiCMS provides flexible and intuitive template tags, helping us easily display these important data at various corners of the website without delving into complex backend logic.

Display document views and comment count on the document detail page

When you want to display the current page views and total number of comments on a single document's detail page (such as an article, a product detail page), AnQiCMS'sarchiveDetailTags are your preferred tool. This tag can retrieve detailed information about the current page document, including preset fields for the number of views and comments.

To display the document views, you can useViewsfield. For example, you can refer to it in the template like this:

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

Alternatively, if your template can directly access the document object, it can also be written more succinctly:.

<span>阅读量:{{archive.Views}}</span>

Similarly, to display the number of comments on the document, you can use:.CommentCountField. The reference method is similar:.

<span>评论:{% archiveDetail with name="CommentCount" %}条</span>

Or:

<span>评论:{{archive.CommentCount}}条</span>

The information is usually placed below the article title, next to the author information, or at the bottom of the page, for the convenience of visitors to see at a glance.

Display the number of views and comments on the document list page

If your website contains an article list, product list, or other document aggregation pages, and you want to display the number of views and comments next to the thumbnail information of each document,archiveListThis tag is very suitable. This tag is used to cyclically output information of multiple documents, where you can access specific attributes of each document within the loop.

When usingarchiveListLabel traverses the document list, and the detailed data of each document will be assigned to the loop variable (usuallyitem). At this time, you can accessitem.Viewsanditem.CommentCountto retrieve and display the corresponding data.

For example, in a template fragment of a document list, you can arrange it like this:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>{{item.Description}}</div>
            <div>
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>阅读:{{item.Views}}</span>
                <span>评论:{{item.CommentCount}}条</span>
            </div>
        </a>
        {% if item.Thumb %}
            <a href="{{item.Link}}">
                <img alt="{{item.Title}}" src="{{item.Thumb}}">
            </a>
        {% endif %}
    </li>
    {% empty %}
    <li>
        该列表没有任何内容
    </li>
    {% endfor %}
{% endarchiveList %}

Through this method, the title, abstract, publication time, corresponding page views, and comment count of each document can be clearly displayed in the list.

Get the total number of comments in the current document's comment area

AnQiCMS provides document details, including the following:CommentCountOutside the field, if you have specifically built a comment list on a certain page and want to display the total number of comments shown on the current page (not the total number of comments in the document, although these two numbers may be the same in some cases), you can usecommentListlabel combined withpaginationtags to achieve this.

commentListThe label itself is used to list comment content, but when it is used withpaginationthe label,paginationthe object will provide aTotalItemsProperties, that is the total number of entries in the current comment list. This is particularly useful when there may be pagination in the comment section.

The following is an example of how to get the total number of comments in the comment section of the current document:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {# 评论列表内容... #}
{% endcommentList %}

{% pagination pages with show="5" %}
    <span>总评论数:{{pages.TotalItems}}条</span>
{% endpagination %}

Here are thearchive.IdRepresents the ID of the current document, ensuring that you get the comments of the document. In this way, you can flexibly display different levels of comment quantity information according to your actual needs.

Practical tips

In actual operation, you can flexibly adjust the display position and style of this information based on the design style and user experience of the website.For example, use CSS to iconize and highlight the number of views and comments, making it more attractive.The template system of AnQiCMS aims to provide high customization, allowing you to easily create website content display effects that match your brand image.

Common Questions (FAQ)

  1. 问:Why does the front-end always show 0 or not update even though I have set the page view display?

    • Answer:Firstly, please confirm that the template tags you are using are correct (for example,{{archive.Views}}or{{item.Views}})。The number of views is usually counted and updated in real-time when users visit the document detail page, and you can try visiting the document detail page several times to trigger the count.If the problem persists, you may need to check if the website's cache is updated in time, or contact the administrator to check if the background statistics function is operating normally.
  2. 问:文档的评论数量是实时更新的吗?新增评论后会立即显示吗?

    • Answer:The number of document comments is dynamically updated.When a new comment is submitted and passed through backend review (if comment review feature is enabled), the number of comments for this document will increase accordingly.The display on the front-end page will vary depending on your page caching strategy. Clear the cache or wait for the cache to refresh to see the latest number of comments.
  3. 问:我能否让浏览量或者评论数量只对特定用户组可见?

    • Answer:AnQiCMS itself does not provide a configuration option for 'view count/comment count visible only to specific user groups'. However, you can achieve this by combiningifThe logic judgment and AnQiCMS's user group management function to implement.For example, determine whether the current user belongs to a certain user group, if so, display the number of views and comments, otherwise hide.This requires a certain ability to write template logic.archive.ReadLevelField, can be used to control the reading level of the document, combined with the user's identity, theoretically, it can also implement more complex permission control.