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)
Why don't my article views and comment numbers show?
- Check the spelling of tags:Make sure you are using in the template
ViewsandCommentCountThe 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:Ensure
archiveDetailtags 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.
- Check the spelling of tags:Make sure you are using in the template
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).
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.
- The numbers displayed themselves are plain text, you can wrap the numbers in HTML elements such as