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

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.