How to implement sorting of the document list by views or publication time using the `archiveList` tag?
As an experienced website operation expert, I deeply understand that the core value of a content management system (CMS) lies in its flexibility and the efficiency of content presentation.AnQiCMS (AnQiCMS) excels in this aspect with its efficient architecture in the Go language and a rich set of features.Today, let's delve deeply into a feature that is extremely commonly used in content operation: how to make use ofarchiveListThe tag implements sorting of the document list by view count or publish time.
Content sorting, it seems simple, but it actually contains profound operational strategies.In order to highlight the latest news or recommend popular content, precise sorting can greatly enhance user experience and content access efficiency.In AnQiCMS,archiveListThe tag is the key tool to achieve this goal.
Get to know AnQiCMS.archiveListTag
First, let's briefly review.archiveListThe label is designed to retrieve and display document lists from the AnQiCMS database, whether articles, products, or documents under other custom content models.Its strength lies in its rich parameters, allowing you to precisely control the range, quantity, and most importantly—the sorting method of the content displayed.
In the AnQiCMS template files, you will see syntax similar to the Django template engine,archiveListtags are usually in the form of{% archiveList archives with ... %}such as this form, among whicharchivesIt is the variable name you define for the document list obtainedwithand the following is various control parameters
The core secret:orderThe magic of parameters
To achieve sorting by page views or publication time, we mainly rely onarchiveListin the labelorderThe parameter is the 'wand' that controls the order of content presentation in AnQiCMS. Its basic syntax isorder="字段名 排序方向".
1. Sort by publication time: let the latest content stand out
On websites where content is updated frequently, the 'Latest Articles' are often the key to attracting users to revisit.AnQiCMS usually associates the creation time of the document with the automatically generated ID (primary key), so the larger the ID, the later the publication time is usually.
To arrange the document list from new to old by publication time, you can useorder="id desc":
{% archiveList latestArticles with categoryId="1" limit="10" order="id desc" %}
{% for item in latestArticles %}
<li>
<a href="{{item.Link}}">{{item.Title}}</a>
<span>发布于: {{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
</li>
{% endfor %}
{% endarchiveList %}
In this code block:
latestArticlesis the variable name we define to store the list of the latest articles.categoryId="1"Specified to only retrieve articles under category ID 1 (you can modify it according to your actual situation).limit="10"Indicates that we want to display the latest 10 articles.order="id desc"It is the core, it tells AnQiCMS to sort by document ID in descending order (descwhich means from large to small), so that the latest released content is displayed at the top.{{stampToDate(item.CreatedTime, "2006-01-02")}}It is used to set the document creation timestampCreatedTimeFormatted into a readable date format, for example “2023-10-26”. AnQiCMS'sstampToDateThe tag is very useful, it follows the Go language's time formatting rules,"2006-01-02"which means年-月-日.
If you need to sort by publish time from old to new, justdescchanged toasc, that isorder="id asc".
2. Sort by views: Discover the most popular content
Understanding user interests and recommending them to more people is an effective means of increasing website activity.By sorting the document list by page views, you can easily create modules such as "Hot Articles" and "Reading Rankings" and more.
To sort the document list by views from highest to lowest, you can useorder="views desc":
{% archiveList hotArticles with categoryId="1" limit="5" order="views desc" %}
{% for item in hotArticles %}
<li>
<a href="{{item.Link}}">{{item.Title}}</a>
<span>浏览量: {{item.Views}}</span>
</li>
{% endfor %}
{% endarchiveList %}
This code:
hotArticlesStored the article list sorted by views.categoryId="1"andlimit="5"The usage is similar to the previous example.order="views desc"It is crucial, it indicates that AnQiCMS sorts the fields according to the document'sViewsfield in descending order to ensure that the documents with the highest number of views are at the top.{{item.Views}}It directly displays the document's view count.
Similarly, if you want to display the document with the least views, you candescchanged toasc, that isorder="views asc".
Combine it with practical use to enhance content operation efficiency
Mastering these two sorting methods, you can apply them flexibly to various operational scenarios:
- The "Latest News" or "This Week's Hot" area on the homepage:Dynamically display the latest or most popular content to maintain the vitality of the website.
- Sidebar "Reading Rank":Guide users to discover more interesting content and extend their stay time.
- Content aggregation page:Provide multiple sorting perspectives for content under specific themes or categories to meet the needs of different users.
- SEO optimization:Combine browsing data to analyze which content is more popular with users and optimize the content strategy accordingly.
By flexible applicationorderParameter, of AnQiCMS.archiveListThe tag provides you with great freedom in content presentation. It not only makes your website content more attractive, but also greatly simplifies the daily work of content operation, allowing you to focus more energy on creating high-quality content.
Frequently Asked Questions (FAQ)
Q1:order="id desc"andorder="CreatedTime desc"What are the differences in AnQiCMS?A1: In AnQiCMS, the document'sidis usually auto-incrementing, thereforeidsize directly reflects the order of document creation. So,order="id desc"In fact, it has the same effect as sorting by creation time from new to old, and is more efficient. AlthougharchiveListthe tags returned byitemincludingCreatedTimefield, butorderthe parameter supports directlyidandviewsand other keyword fields for sorting,CreatedTimeis mainly used for display rather than direct sorting.
Q2: Can I sort by multiple conditions at the same time, such as sorting by views first and then by publish time?A2: AnQiCMS'archiveListlabel'sorderParameters usually accept a primary sorting rule. According to the document example, it listsorder="id desc"/order="views desc"Independent options. Although in the documentdesign-tag.mdappeared in the exampleorder="id desc|views desc"This writing may imply a priority or alternative sorting logic.But in practice, for clarity, we usually choose the most important sorting dimension.If you have more complex sorting requirements, you may need to combine backend logic or further review the deeper documentation of AnQiCMS to confirm the specific support methods and syntax for multi-level sorting.
Q3: How to display more document information in the sorted list, such as category names or custom fields?A3: InarchiveListlabel'sforthe loop,itemThe variable represents the current document object, you can directly{{item.字段名}}access its properties, for example,{{item.Title}}/{{item.Link}}/{{item.Views}}. To access the category name of the document, you can{{ categoryDetail with name="Title" id=item.CategoryId }}Nested tags to obtain. For example, if the document model has custom fields,authoryou can also use{{item.author}}or more generalarchiveParamstags to call and display this additional information.