How to display the page views and comment count of the document?

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

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

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

To display the document's view count, you can useViewsfield. For example, you can reference it in the template like this:

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

Or, if your template can directly access the document object, it can also be written more concisely:

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

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

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

OR:

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

Generally, this information is placed below the article title, next to the author's information, or at the bottom of the page, so that visitors can see it at a glance.

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

If your website contains a list of articles, product lists, or other document aggregation pages, and you want to display the number of views and comments next to the thumbnail information of each document,archiveListThe tag is very applicable. This tag is used to loop out information of multiple documents, you can access the specific properties of each document within the loop.

While usingarchiveListWhen traversing the document list, 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 snippet of a document list, you can lay it out 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 %}

In this way, the title, summary, publication time, and corresponding number of views and comments of each document can be clearly displayed in the list.

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

AnQiCMS in addition to providing document details,CommentCountOutside the field, if you have built a comment list on a specific page and want to display the total number of comments displayed on the current page (not the total comment count of the document, although these values may be the same in some cases), you can usecommentListLabel collaborationpaginationthe tag.

commentListThe tag itself is used to list comment content, but when it is used withpaginationthe tag together,paginationthe object will provide aTotalItemsThe property, which is the total number of items in the current comment list. This is particularly useful for comment sections that may have pagination.

Here is an example of how to get the total number of comments in the current document's comment section.

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

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

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

Useful Tips

In practice, you can consider the design style of the website and user experience to flexibly adjust the display position and style of this information.For example, use CSS to iconize and highlight reading volume and comment count, making it more attractive.The AnQiCMS template system aims to provide high customizability, allowing you to easily create website content display effects that match your brand image.

Frequently Asked Questions (FAQ)

  1. Why is it that when I set the page view display, the frontend always shows 0 or does not update?

    • Answer:First, please confirm that the template tag you are using is correct (for example{{archive.Views}}or{{item.Views}})。The page views are 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, it may be necessary to check if the website's cache is updated in time, or contact the administrator to check if the backend statistics function is operating normally.
  2. Ask: Is the number of comments on the document updated in real time? Will new comments be displayed immediately?

    • Answer:The number of comments on the document is dynamically updated. When new comments are submitted and approved by the backend (if comment approval is enabled), the number of comments on the document will increase accordingly.The display on the front-end page will vary according to your page caching strategy, clear the cache or wait for the cache to refresh to see the latest number of comments.
  3. Ask: Can I make the number of views or comments visible only to a specific user group?

    • Answer:AnQiCMS itself does not provide a direct configuration option for "View count/Comment count visible only to specific user groups". However, you can combine it withifThe logic judgment and user group management function of AnQiCMS can be implemented.For example, determine if 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. AnQiCMS supportsarchive.ReadLevelField, can be used to control the reading level of the document, combined with user identity, theoretically it can also realize more complex permission control.