In website operation, the number of page views of articles is a very intuitive and important indicator. It not only reflects the popularity of the content but also provides data support for the optimization of subsequent content strategies.For websites built with AnQiCMS, displaying the number of views in the article list or detail page is a relatively simple operation. With the powerful template tag features provided by AnQiCMS, we can easily achieve this requirement.
Understanding AnQiCMS's “Page Views” feature
AnQiCMS内置了强大的流量统计功能,系统会自动记录每一篇文章、产品或页面的访问次数。This feature is part of AnQiCMS's core highlight of 'Traffic Statistics and Spider Monitoring', aiming to help users fully understand the performance of the website.This means that we can display this valuable data on the frontend page without any additional configuration, allowing visitors to see the popularity of the content.
Displaying page views on the article detail page
When you visit a specific article, you will enter its detail page. On this page, AnQiCMS providesarchiveDetailTag to get the detailed information of the current article. To display the number of views of the current article, you just need to add it to the template file of the detail page (for examplearchive/detail.htmlor the custom template you have created for a specific content modelyour-model/detail.html)Find a suitable position within, and then embed the corresponding tag.
The specific code snippet is very concise:
{% archiveDetail with name="Views" %}
Generally, we would like to add some descriptive text to the number of views, such as “Reads:” or “Views:”, so that visitors can clearly understand the meaning of this number. You can combine it like this:
阅读量:{% archiveDetail with name="Views" %}
You can also choose to assign the number of views to a variable and then perform subsequent operations or display it. For example, if you want to add a unit 'times' to the number of views, you can handle it like this:
{% archiveDetail archiveViews with name="Views" %}
浏览次数:{{ archiveViews }} 次
Choose a suitable position on the page, such as below the article title, next to the publication time, or near the author information, and add this code in. After saving the template file and refreshing the page, you will be able to see the current article's page views.
Display views on the article list page
On the article list page, such as the article recommendation area on the homepage, the category list page, and even the search results page, displaying the page views of each article can also enhance the user experience.Visitors can easily see which content is more popular, helping them make click choices.
AnQiCMS provides for thisarchiveListTags, used for looping to display articles.archiveListThe loop body of the label, the data of each article will be assigned to aitemvariable. You can accessitem.Viewsto get the number of views of the article.
You can do this onarchive/list.htmlor other related template files (such asindex/index.html的文章模块部分)中找到{% archiveList ... %}的循环区域,通常会是{% for item in archives %}或类似的结构。然后将代码片段嵌入其中:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<article>
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description }}</p>
<footer>
<span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
<span class="views-count">浏览量:{{ item.Views }}</span> {# 在这里显示浏览量 #}
</footer>
</article>
{% empty %}
<p>暂时没有文章。</p>
{% endfor %}
{% endarchiveList %}
In the above example,item.ViewsWill output the page views of each article. You can place it in the most suitable position according to your template structure.<span>浏览量:{{ item.Views }}</span>Place it in the most suitable position.
Tips in actual operation
- [en] Style beautification:Purely numerical display may not be aesthetically pleasing, you can use CSS to beautify the style, such as adding icons to the number of views, adjusting the font size or color, so that it matches the overall design style of the website.
- Default Value Handling:Sometimes, newly published articles may not have generated any page views yet, or there may be a delay in data statistics.To avoid displaying blank spaces, you can consider using the AnQiCMS filter to set default values.
defaultFilter, whenitem.ViewsShow "0" when empty浏览量:{{ item.Views|default:"0" }} - Enhance user experience:Displaying the page views is not just for display; it can also guide users to discover popular content, stimulate user interaction (such as comments), and even serve as an indirect indicator of search engine optimization (SEO) in some cases, as high page views usually mean higher content quality, which is helpful for improving the overall performance of the website.
Through these simple operations, you can easily display the article page views on the website built with AnQiCMS, providing visitors with more information and bringing more data insights for website operation.
Common Questions (FAQ)
Why is my article view count showing as 0 or not displayed?This usually has several reasons.Firstly, the newly published article may indeed have not generated any views yet; secondly, it may be due to caching reasons that AnQiCMS's caching mechanism may cause the front-end page to fail to update the view count data in time. You can try to 'update the cache' in the background or wait for a while before checking.最后,如果上述情况都不是,请检查你的模板代码是否正确,标签名称是否拼写无误。
Is the view count data updated in real time? Can I manually modify or reset the view count?AnQiCMS's page view data will increase in real-time with each page visit.This means that each visit by the visitor will be counted.Regarding manual modification or reset of page views, AnQiCMS backend usually does not provide a direct manual modification feature for single article page views, which is to ensure the authenticity and accuracy of the data.If there are special requirements, it may be necessary to perform operations at the database level, but this is not recommended for ordinary users to try, in order to avoid data chaos.
Can the article be sorted according to the number of views?Yes, AnQiCMS's
archiveListTags support multiple sorting methods, including sorting by the number of views. You can inarchiveListtag.order="views desc"Parameters, to implement the display of article lists sorted by views in descending order. For example:{% archiveList archives with type="list" order="views desc" limit="10" %}This way, you can easily display the most popular articles on the website.