How to implement a 'Hot Articles' or 'Most Viewed' list, achieved through the `order` parameter?

Calendar 👁️ 93

In website operation, we all hope to display the most popular and highly read articles to visitors, such as common lists like 'Hot Articles' or 'Most Viewed'.This not only effectively guides users to discover more exciting content and enhance the user experience of the website, but is also an indispensable part of content operation.In Anqi CMS, implementing such a feature is simpler than you imagine, the secret is hidden in the document list interfacearchive/listoforderIn the parameters.

Use cleverlyorderParameters, create your popular content list

AnQi CMS provides a powerful and flexible document list interface/api/archive/listIt allows us to filter and sort articles based on various conditions. Among them,orderThe parameter is the core of implementing the "Popular Articles" or "Most Viewed" list.

In simple terms,orderThe parameter is used to tell the system the rule you want the article list to be sorted by. For the "most viewed" feature we want to implement, the most direct way is to use the article's view count (viewsField) to sort in descending order. The Anqi CMS system automatically counts the page views of each article, and we just need to sort throughorderspecify the parameter to sort by this field.

For example, if you want to get the article with the highest views, justorderthe parameter toviews desc. Here,viewsrefers to the article view field, anddescThis indicates sorting in descending order, that is, from high to low. As a result, the returned article list will be sorted from the most viewed to the least viewed.

Gradually implement the "Hot Articles" list

Next, let's take a look at how to use a few simple steps toorderparameters to retrieve and display the most popular articles on your website.

Step one: clarify your needs

Before constructing the request, we need to think clearly about several issues:

  • Which module's articles do you want to retrieve (such as news articles or product introductions)? This corresponds to:moduleIdParameter.
  • Do you want to limit the hot articles to a specific category? This corresponds tocategoryIdParameter.
  • How many hot articles do you want to display on the page? This corresponds tolimitParameter.

Step two: Construct the API request URL

With clear requirements, we can start building the API request URL. Suppose we want to get the article module (moduleId=1Under it, the top 10 articles in terms of views, the request URL will be like this:

{域名地址}/api/archive/list?moduleId=1&limit=10&order=views desc

Remember to replace it with your actual website domain name, for example:{域名地址}Replace it with your actual website domain name, for example:https://www.yourwebsite.com.

  • moduleId=1: Indicates retrieving the document under the article model with ID 1.
  • limit=10: Indicates retrieving only the first 10 articles.
  • order=views desc: This is the core, it tells Anqi CMS to follow the rule ofviewsThe field (views) is sorted in descending order.

If you need to specify a category, such as articles with category ID 5, just addcategoryId=5:

{域名地址}/api/archive/list?moduleId=1&categoryId=5&limit=10&order=views desc

Step 3: Process the returned data

After you send a GET request to this URL, AnQi CMS will return a JSON formatted data. In the case of success, you will seecodeis 0,dataThe field contains a list of articles. Each article object includesid/title/views/linketc. detailed information.

You need to write code to parse this JSON response. For example, using JavaScript'sfetchoraxioslibrary to make a request, then traversedatathe array to extract the article information you need.

The fourth step: Display on the page

Next, you can use this data to render a 'Popular Articles' or 'Most Viewed' list on your website.This is usually a simple unordered list or ordered list, each item including the article title and a link to the article details page.

For example, a simplified HTML structure may look like this:

<div class="hot-articles">
    <h3>热门文章</h3>
    <ul>
        <!-- 这里会通过JavaScript动态插入文章列表 -->
    </ul>
</div>

<script>
    fetch('/api/archive/list?moduleId=1&limit=10&order=views desc')
        .then(response => response.json())
        .then(data => {
            if (data.code === 0 && data.data) {
                const ul = document.querySelector('.hot-articles ul');
                data.data.forEach(article => {
                    const li = document.createElement('li');
                    const a = document.createElement('a');
                    a.href = article.link; // 文章详情页链接
                    a.textContent = article.title + ' (' + article.views + '浏览)'; // 文章标题和浏览量
                    li.appendChild(a);
                    ul.appendChild(li);
                });
            } else {
                console.error('获取热门文章失败:', data.msg);
            }
        })
        .catch(error => {
            console.error('请求热门文章出错:', error);
        });
</script>

This code will request popular article data and generate a list on the page that includes titles and views, with each title linking to the corresponding article detail page.

Advanced skills and thinking

  • Implement “Latest Articles”:If you want to display a list of “Latest Articles”, you can:orderparameter tocreated_time descThis will sort the articles by their publishing time from new to old.
  • Combined sorting:AlthoughorderParameters are usually used for sorting a single field, but in some advanced scenarios, you may need to combine multiple conditions. For example, you can first sort byviews descand then sort bycreated_time descThis requires the backend interface to support sorting strings by multiple fields, the document example of AnQi CMS is a single field, and when in use, please refer to the latest API document.
  • Performance optimization:Consider caching frequently requested lists on the front end or through CDN to reduce server pressure and improve loading speed.
  • Front-end framework integration:If you use front-end frameworks such as Vue, React, etc., you can integrate the data acquisition and rendering logic into components to achieve more flexible and efficient display.

By flexibly using Anqi CMSarchive/listthe interface inorderParameters, you can easily add various sorted article lists to your website, whether it's 'Hot Articles', 'Latest Release' or other custom sorting, you can handle it with ease.This convenience greatly improves the display efficiency and user experience of the website, making your website more attractive.

Frequently Asked Questions (FAQ)

Q1:orderParameters exceptviewsandcreated_timeWhat fields does it support for sorting? orderParameters support various built-in field sorting, common ones include:id(article ID),views(Views),category_id(Category ID),created_time(creation time),updated_time(Update time) and so on. You can choose the appropriate fields according to your needs and combineasc(Ascending) ordesc(Descending) to sort.

Q2: If I want to define 'hot' not only based on views, but also want to sort by the number of comments, is that possible?According to the current documentation,orderThe parameters mainly supportid/views/category_id/created_time/updated_timeThese fields. If you want to sort by comments (comment_count), you need to consult the latest Anqi CMS API documentation to confirm.comment_countWhether it has been added to the sortable field. If not supported, you can consider performing data secondary processing on the backend, or using a custom field to record the 'hot score'

Related articles

What is the default behavior and return content of the `archive/list` interface when no parameters are passed?

## In-depth Analysis of AnQiCMS `archive/list` Interface: Default Behavior and Return Content when No Parameters Are Provided When developing or managing websites with AnQiCMS, we often need to obtain various data through API interfaces.The `archive/list` interface is one of the core interfaces for obtaining the document list of the website.Understanding its default behavior and return content when no parameters are passed is crucial for efficient data acquisition and initial debugging.

2025-11-09

How to get the articles published by a specified user (`user_id`) through the AnQiCMS document list?

In website content operation, we often need to display all articles of a specific author, such as on the author's personal homepage, or on a special topic page aggregating the content of specific contributors.AnQiCMS (AnQiCMS) provides a powerful and flexible API interface, allowing us to easily meet this requirement. To get the list of all articles published by a specified user (`user_id`), we need to use the `archive/list` interface.

2025-11-09

What is the significance of the `price` and `stock` fields returned by the `archive/list` interface for e-commerce document models?

In AnQi CMS, when you retrieve the document list through the `archive/list` interface, the returned data includes the `price` and `stock` fields, which are of vital importance for constructing an e-commerce website model.They are not just simple numbers, but are the core elements supporting product display, transaction process, and inventory management.

2025-11-09

I want to display the number of comments for each article on the AnQiCMS document list page, which field should I check?

In the daily use of AnQiCMS, many operators hope to be able to directly display the number of comments for each article on the article list page.This not only effectively enhances the activity of the content, but also helps users quickly understand which articles are more popular and have more discussion value.Then, among all the data fields, which one should we focus on in order to meet this requirement?

2025-11-09

What will `data` and `total` return if no documents meeting the criteria are found in the AnQiCMS document list?

When building a website or application with AnQiCMS, we often need to use its provided API interface to retrieve various content, such as document lists.What data structure will the API return when our query conditions do not match any content?Especially the `data` and `total` fields, which are crucial for our proper data handling.Today, let's delve into how AnQi CMS responds when it does not find a document that meets the criteria in the document list.

2025-11-09

What is the help of `archive/list` interface returned `canonical_url` and `fixed_link` fields to SEO optimization?

In the ocean of website content, how can our high-quality content stand out and be discovered by more potential users, which is a topic that every content operator continuously explores.Search engine optimization (SEO) is one of the key strategies to achieve this goal.In SEO practice, the URL plays an extremely important role.

2025-11-09

How to use the `archive/list` interface to dynamically load more documents on the front end (infinite scrolling)?

In modern web design, infinite scrolling has become a popular content loading method that significantly enhances user experience, allowing visitors to maintain immersion while browsing content without interruption.For users who build websites using AnQiCMS, the `archive/list` interface is a powerful tool to achieve this function.

2025-11-09

Does the AnQiCMS document list interface support complex queries on the returned data's `extra` field?

In Anqi CMS, the flexibility of document content management is a highly关注的 feature, especially its support for custom fields (manifested in the `extra` field returned by the interface), providing great convenience for website operators.When we need to filter and query a large number of documents based on these custom properties, we naturally think of a key question: Does the Anqi CMS document list interface (`/api/archive/list`) support more complex queries on the `extra` field in the returned data?

2025-11-09