How can articles be sorted by views in the AnQiCMS template?

Calendar 👁️ 74

In AnQiCMS, content management is not limited to publishing and organizing, how to efficiently display these contents, especially placing popular articles in a prominent position, is the key to improving user experience and website activity.This article will provide a detailed introduction on how to configure the AnQiCMS template to sort the article list by views, making popular content naturally stand out.

Understand the Core:archiveListwith the tag andorderParameter

AnQiCMS provides a powerful and flexible template tag system, wherearchiveListTags are the core tools for retrieving article lists. The key to achieving sorting by page views lies in an important parameter of this tag:order.

orderThe parameter allows you to specify the sorting rule for the article list. When you want the articles to be sorted by views in descending order, justorderthe parameter toviews desc.viewsHere it refers to the article view field, anddescDescending, meaning the articles with the highest views will be displayed at the top of the list. If you want to sort the views from low to high, you can useviews asc.

How to implement sorting by page views in the template

Next, we demonstrate how to use it through a real template code examplearchiveListtags and theirorderParameter.

Assume you want to display the 10 most popular articles on a page, and these articles can be paginated, then your template code may look like this:

{# 使用 archiveList 标签获取文章列表,并指定按浏览量降序排列 #}
{% archiveList popularArticles with type="page" order="views desc" limit="10" %}
    <div class="article-list">
        {# 循环遍历热门文章列表 #}
        {% for item in popularArticles %}
        <article class="article-item">
            <a href="{{ item.Link }}" title="{{ item.Title }}">
                {% if item.Thumb %}
                <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                {% endif %}
                <h3 class="article-title">{{ item.Title }}</h3>
                <p class="article-meta">
                    <span>发布于:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    <span>浏览量:{{ item.Views }}</span>
                </p>
                <p class="article-description">{{ item.Description }}</p>
            </a>
        </article>
        {% empty %}
        <p class="no-content-tip">暂时没有热门文章,请等待内容发布或浏览量数据更新。</p>
        {% endfor %}
    </div>

    {# 如果列表类型设置为 "page",您还可以使用分页标签来生成分页链接 #}
    {% pagination pages with show="5" %}
        <nav class="pagination-nav">
            {# 首页链接 #}
            <a href="{{ pages.FirstPage.Link }}" class="page-link {% if pages.FirstPage.IsCurrent %}active{% endif %}">首页</a>
            {# 上一页链接 #}
            {% if pages.PrevPage %}
            <a href="{{ pages.PrevPage.Link }}" class="page-link">上一页</a>
            {% endif %}
            {# 中间页码链接 #}
            {% for pageItem in pages.Pages %}
            <a href="{{ pageItem.Link }}" class="page-link {% if pageItem.IsCurrent %}active{% endif %}">{{ pageItem.Name }}</a>
            {% endfor %}
            {# 下一页链接 #}
            {% if pages.NextPage %}
            <a href="{{ pages.NextPage.Link }}" class="page-link">下一页</a>
            {% endif %}
            {# 尾页链接 #}
            <a href="{{ pages.LastPage.Link }}" class="page-link {% if pages.LastPage.IsCurrent %}active{% endif %}">尾页</a>
        </nav>
    {% endpagination %}
{% endarchiveList %}

In the code above,popularArticlesIs the variable name you defined, used to store the list of retrieved articles.type="page"Indicates that this is a paged list, whilelimit="10"limits the display of 10 articles per page. The most important thing isorder="views desc"It ensures that the articles are sorted from highest to lowest in terms of views. Inside the loop, you can use{{ item.Views }}to directly display the views of each article.

Flexibly use other parameters

In addition to sorting by views,archiveListTags also support combining with other parameters to achieve more refined content display control:

  • categoryId: If you want to display popular articles under a specific category, you can addcategoryId="您的分类ID". If this parameter is omitted,archiveListAttempts to read the category ID of the current page; to get articles from the entire site, you can explicitly set it tocategoryId="0".
  • limit: Controls the number of articles displayed. You can specifylimit="5"to display only 5 hot articles.
  • type: Besides"page"In addition to pagination display, you can also use"list"to get a fixed number of items without pagination, which is often used in sidebars or hot recommendation areas on the homepage.

By these flexible parameter combinations, you can precisely control the display style of articles according to the different needs of website design, ensuring that users can always find the popular content they are interested in.

Summary

In AnQiCMS template, sorting the article list by views and displaying it is a simple and efficient content operation strategy. UtilizingarchiveListlabel'sorder="views desc"Parameters, you can easily display the most popular articles on the website in front of users, thus optimizing the content layout and improving user stickiness. Combined with other parameters such ascategoryIdandlimitYou can build a rich and user-friendly article list display module.


Frequently Asked Questions (FAQ)

  1. Besides browsing volume, what other ways can I sort the article list?AnQiCMS'archiveListTags support multiple sorting methods. Common ones include:

    • Sort by the latest published time: order="id desc"(Default sorts in descending order by article ID, usually consistent with the published time)
    • Sort manually in the admin backend: order="sort desc"
    • Sort by publish time in descending order: order="createdTime desc"
    • Sort by publish time in ascending order: order="createdTime asc"You can also combine multiple sorting conditions, for example:order="views desc|id desc"The articles are first sorted in descending order by views, and if the views are the same, they are then sorted in descending order by article ID (i.e., the most recent published).
  2. How to display only the popular articles under a specific category?Sort articles by views in a specific category, justarchiveListthe tag withcategoryIdset the parameter. For example, to display{% archiveList popularArticles with categoryId="5" order="views desc" limit="10" %}.

  3. Why did I setorder="views desc"But the article view count did not display?settingorder="views desc"It will only affect the sorting method of the article and will not automatically display view count data. You need to manually iterate through the article list.{{ item.Views }}Output the view count of each article. Make sure your template includes{{ item.Views }}this line of code, so that the view count can be displayed on the page.

Related articles

How does AnQiCMS allow search results to be paginated on the page?

In AnQiCMS, implementing pagination for the website's search results is an important function for improving user experience and optimizing the website structure.AnQiCMS's powerful template tag system makes this process intuitive and efficient.By reasonably utilizing the `archiveList` and `pagination` core tags, you can easily build a functional search results page.

2025-11-08

How to implement pagination functionality for article or product lists in AnQiCMS templates?

How to implement pagination for article or product lists in AnQiCMS template?For any content management system, effectively managing and displaying a large amount of content is a core requirement.When the number of articles or products on a website increases day by day, if there is no reasonable pagination mechanism, users may feel tired while browsing, and the website's loading speed may also be affected.

2025-11-08

How does AnQiCMS filter and display content based on article recommendation attributes (such as headline, recommended)?

In content operation, effectively highlighting and displaying important articles is the key to improving user experience and guiding traffic.AnQiCMS (AnQiCMS) fully understands this point, therefore, it provides a flexible article recommendation feature, allowing you to mark it based on the importance of the content or specific use, and accurately filter and display it on the website front end.

2025-11-08

How to exclude content of a specific category from the article list?

In website content operation, we often encounter such needs: we hope to display most of the content on the article list page, but articles of certain specific categories are not listed here.For example, you may have a "Company Announcement" category that you only want to display on a separate page, or some articles for internal reference that you do not want to appear in the regular article list facing the public.AnQiCMS (AnQiCMS) provides a very flexible and intuitive way to handle this situation, allowing you to accurately control the display of content on the front end.The Anqi CMS through its powerful template tag system

2025-11-08

How to display a list of articles published by a specific author in AnQiCMS?

In AnQiCMS, efficiently managing and displaying content is the core of website operation.Sometimes, you may need to display all the articles published by a specific author, such as creating an author column, a team member introduction page, or a personal portfolio.AnQiCMS's powerful template tag system provides a flexible way to meet this requirement, allowing you to easily filter and display a list of content from specific authors.This article will introduce you to how to display a list of articles published by a specific author in AnQiCMS, helping you better organize your website content.##

2025-11-08

How to filter and display a list of documents in AnQiCMS using custom parameters?

In AnQiCMS (AnQiCMS), when managing and displaying a large amount of content, we often need to go beyond simple categories and tags to achieve more refined content filtering.For example, a real estate website may need to filter listings by "type", "area", and "price range";An e-commerce website may need to filter products by color, size, and brand.AnQiCMS provides a powerful custom parameter feature, making it very intuitive and efficient to implement this advanced filtering.Below, we will gradually understand how to use AnQiCMS

2025-11-08

How to display all documents under the current category and its subcategories in AnQiCMS template?

In AnQiCMS template design, flexibly displaying content is the key to website operation.When you need to display articles under a category page not only, but also hope to present all subcategory articles together, or display them in a more structured way, AnQiCMS provides powerful template tags to meet these needs.This article will introduce in detail how to implement this feature in your AnQiCMS template.### Understanding AnQiCMS's classification and document mechanism In AnQiCMS, content is usually organized under "document model" and "classification"

2025-11-08

How to get and display the detailed information of a single article in AnQiCMS?

Managing and displaying content in AnQiCMS is one of its core functions.It is crucial to understand how to accurately retrieve and display the detailed content of a specific article when you need to present it to visitors.AnQiCMS provides a直观 and powerful template tag system that allows you to flexibly control the layout and content of the article detail page. ### Overview of AnQiCMS Template System The AnQiCMS template uses syntax similar to the Django template engine.In the template file, you will encounter two main markers: - **Double braces

2025-11-08