As an experienced website operations expert, I am well aware that effectively displaying 'Latest Documents' and 'Most Viewed' content in a vast amount of information is crucial for attracting users and enhancing website activity.Auto CMS (AutoCMS) provides us with the tools we need to achieve this goal with its powerful template system and flexible tag features.tagDataListHow to cleverly implement these two common sorting requirements of tags.

AnQiCMS in EnglishtagDataListTags: The powerful tool of content operation.

Firstly, let's understand it.tagDataListTags. In AnQiCMS,tagDataListThe label is specifically used to retrieve the document list associated with a specific 'label (Tag)'. This means that if you want to display the latest articles or the most popular products under a specific theme, tagDataList标签是你的首选。它不仅能够检索内容,更关键的是,它还提供了丰富而直观的排序机制,让内容呈现更加智能。

use cleverlyorder参数:Unlock the "Latest Documents" and "Most Viewed"

tagDataListThe core of the tag lies in its powerfulorderParameters. This parameter allows us to specify the display order of documents, thereby easily achieving the common sorting requirements of "Latest Documents" and "Most Viewed

Implement "Latest Documents" sorting

To display the "latest documents" under a certain tag, we usually want the content to be sorted from new to old by publishing time. In AnQiCMS, the unique identifier ID of the document isid)is typically auto-incrementing, which means the publishing time of documents with larger IDs is later. Therefore, we can achieve this by sorting theidfield in descending orderdesc)to reach the goal.

We cantagDataListtags as followsorderParameters:order="id desc"This will tell AnQiCMS to return data in descending order of document ID. Naturally, your website visitors will see the latest content.

For example, if you want to display the 5 latest articles under a specific tag on your tab page, you can write the template code in this way:

{% tagDataList latestArticles with tagId="您的标签ID" order="id desc" limit="5" %}
    <div class="latest-articles-list">
        <h3>最新文章</h3>
        <ul>
            {% for article in latestArticles %}
            <li>
                <a href="{{ article.Link }}">{{ article.Title }}</a>
                <span>发布日期: {{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
            </li>
            {% endfor %}
        </ul>
    </div>
{% endtagDataList %}

In this code block:

  • tagId="您的标签ID"Please replace it with the actual label ID, or if you use it on the label detail page,tagDataListit will automatically get the label ID of the current page.
  • order="id desc"This is the key to implementing the "latest document", ensuring that the latest content is displayed at the top.
  • limit="5"Limit to displaying the latest 5 articles only.
  • stampToDate(article.CreatedTime, "2006-01-02")This is a very useful time formatting function, which converts the document's timestamp into a readable date format.

Implement the 'Most Viewed' sorting.

For the "most viewed" content, what we care about is the popularity of the content. AnQiCMS records the number of views for each document,views)Data. Therefore, to display the most popular content, we can sort the field in descending order.viewsfield in descending order.

IntagDataListtags, we willorderparameter settingsorder="views desc". In this way, the system will return the content in order of the number of times the document has been viewed, from high to low, so that you can see your popular content at a glance.

If you want to display the top 5 articles with the most views under a certain tag:

{% tagDataList popularArticles with tagId="您的标签ID" order="views desc" limit="5" %}
    <div class="popular-articles-list">
        <h3>热门文章</h3>
        <ul>
            {% for article in popularArticles %}
            <li>
                <a href="{{ article.Link }}">{{ article.Title }}</a>
                <span>浏览量: {{ article.Views }}</span>
            </li>
            {% endfor %}
        </ul>
    </div>
{% endtagDataList %}

Here:

  • order="views desc"This is the key to implementing 'most views': make sure the most popular content is at the top.
  • article.ViewsEnglish: Directly displays the document's view count data.

Through such simple parameter adjustment, you can flexibly display various sorted content lists in different areas of the website, greatly enhancing user experience and content discoverability.

More advanced usage and tips

In addition to basic sorting,tagDataListit also supports combining other parameters to achieve more refined control:

  • Limit the number of displayed items: Uselimit="N"Parameters, you can accurately control the number of items displayed in the list, for examplelimit="10". You can even specify the starting position and quantity, such aslimit="2,10"This means to take 10 records starting from the 2nd one.
  • Specify the content model:If you want to get content with a specific tag under a particular content model (such as article model or product model), you can usemoduleIdparameter, such asmoduleId="1"(The article model usually has an ID of 1).
  • Display in pagesIf the content is too much, you want to display it in pagination, you cantypeparameter settingstype="page"Then combine it with AnQiCMS'spaginationtags to implement pagination.

Benefiting from the clear template syntax and powerful function design of AnQiCMS, website operators can achieve a rich and diverse content display strategy without delving into complex backend code by adjusting the template tag parameters.This not only improves work efficiency, but also makes content operation more skillful.

Common Questions and Answers (FAQ)

  1. Question: Besidesid descandviews desc,tagDataListWhat other sorting methods do you support?Answer:tagDataListSupports descending order sorting by custom sorting fields in the background, that is,order="sort desc". ThissortFields allow you to manually adjust the priority of documents in the background to achieve more flexible personalized sorting requirements.

  2. 问:我可以直接使用tagDataList来获取所有文档的最新或最热列表,而不是特定标签下的文档吗?Answer: No.tagDataListIt stands for "Tag Data List" as the name implies, it always associates with one or more tags. If you want to getEntire SiteThe latest document or the most popular document, not limited to a specific tag, you should use the document provided by AnQiCMSarchiveListtag, which also supportsorder="id desc"andorder="views desc"parameter, and no need to specifytagId.

  3. 问:如果我的文档没有浏览量数据,order="views desc"how will it be displayed?答:如果文档没有浏览量数据(即views字段为0或为空),它们在views descSorted documents with views will be placed after those without, and their relative order will be determined by other default sorting rules (usuallyid desc)to decide. It is recommended to ensure that your documents have correct page view statistics to obtain accurate "hot" sorting results.