How does AnQiCMS sort articles by view count (Views) and display the most popular articles?

How to effectively display the most popular articles to visitors in website operation, which is a key factor in enhancing user engagement and optimizing content strategy.When visitors can easily find the hot content they are interested in, not only can it extend their stay on the website, but it can also effectively promote the spread of content.AnQiCMS provides a flexible and powerful template tag system that allows website administrators to easily sort articles by page views and display the most popular content without complex technical operations.

Revealing the Core Functionality: AnQiCMS Template Tag System

AnQiCMS is a content management system developed based on the Go language, one of its advantages is its simple and efficient template engine.It borrowed the syntax of Django template engine, through a series of rich template tags, allowing content developers to obtain and display website data in a直观 way.archiveListThis powerful document list label.

archiveListLabels can filter and sort articles based on various conditions, including but not limited to the category of the article, recommendation attributes, publication time, and even custom parameters. Today's focus is on how to utilize its sorting function (orderParameters) to achieve the reverse sorting by (Views), thereby finding the most popular articles.

Steps to implement the sorting of popular articles.

To display the popular articles on the website, it can be divided into the following steps:

Step 1: Determine the location of the template file

First, you need to determine which page to display popular articles. Usually, popular articles will appear on the homepage of the website (index.html), in the sidebar (possibly a public fragment file, such aspartial/aside.html)、an article list page area(such asarchive/list.html)or a special hot article page. AnQiCMS template files are usually located at/templateUnder the directory, you can select or create a new template file for editing as needed.

Second step: usearchiveListTags retrieve article data

We will use the selected template file,archiveListTag to call article data.This tag allows us to specify multiple parameters to accurately control the list of articles obtained.

  1. order="views desc"This is the core parameter for AnQiCMS to sort articles by views (Views) in descending order (desc).viewsRepresents the article view field,descIndicates reverse order, that is, the view count is arranged from high to low.
  2. limit="N"This parameter is used to limit the number of articles displayed. For example, if you want to display the top 10 most popular articles, you can set it tolimit="10".

Moreover, since we usually want to display these popular articles directly in a list, rather than paginating, we cantypethe parameter totype="list".

In summary, the code snippet to get the tags of popular articles might look like this:

{% archiveList archives with type="list" order="views desc" limit="10" %}
    {# 遍历文章列表的代码将放在这里 #}
{% endarchiveList %}

here,archivesIt is a custom variable name used to store the article list data returned by AnQiCMS.

Step 3: Display article information

After obtaining the article list data, we need to use aforto loop througharchivesVariable, and display the title, link, and view count of each article. Within the loop, each article object is usually nameditemYou can useitem.属性名Visit the various data of the article.

Some commonly used article properties include:

  • item.Title: Article title
  • item.Link: Article link
  • item.Views: Article views
  • item.Description: Article Summary
  • item.Thumboritem.Logo: Article Thumbnail
  • item.CreatedTime: Article Publish Time (Timestamp, usually required to be parsed throughstampToDateTag Formatting)

Complete Code Example with Explanation

Here is a complete example code that shows how to display the top 10 articles in the AnQiCMS template, including titles, links, views, and thumbnails (if available):

<div class="popular-articles">
    <h2>最热门文章</h2>
    <ul>
        {% archiveList archives with type="list" order="views desc" limit="10" %}
            {% for item in archives %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">
                        {% if item.Thumb %}
                            <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                        {% endif %}
                        <div class="article-info">
                            <h3 class="article-title">{{ item.Title }}</h3>
                            <p class="article-views">浏览量:{{ item.Views }}</p>
                            <p class="article-date">发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
                        </div>
                    </a>
                </li>
            {% empty %}
                <li>暂时没有文章可显示。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Code analysis:

  • <div class="popular-articles">...</div>This is an outer container used to wrap the list of popular articles for easy style control.
  • <h2>最热门文章</h2>Displays a title to clearly indicate that this section contains popular articles.
  • {% archiveList archives with type="list" order="views desc" limit="10" %}:
    • InvokearchiveListLabel, and assign the retrieved article list toarchivesVariable.
    • type="list": means we want a simple list instead of one with pagination features.
    • order="views desc": This is the key! It tells the system to sort the articles byViewsField (views) sorted in descending order.
    • limit="10": Limit to display only the top 10 articles.
  • {% for item in archives %}: Loop through.archivesEach article's data, each article's data is stored in.itemthe variable.
  • <a href="{{ item.Link }}" title="{{ item.Title }}">...</a>For each article, create a link pointing to the article detail page, and useitem.Titleas the link title hint.
  • {% if item.Thumb %}<img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">{% endif %}To determine if an article has a thumbnail (item.Thumb), and if so, display the image.
  • <h3 class="article-title">{{ item.Title }}</h3>Display article title.
  • <p class="article-views">浏览量:{{ item.Views }}</p>Display article views.
  • <p class="article-date">发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>: Use.stampToDateTagged with the Unix timestamp of the article's publish time.item.CreatedTimeFormatted as年-月-日in the form of.
  • {% empty %}This isforAn optional sub-sentence of the loop. IfarchivesThe list is empty (i.e., no articles), it will display<li>暂时没有文章可显示。</li>.
  • {% endfor %}and{% endarchiveList %}: are used to endforloop andarchiveList.

Further optimization and customization of the display

  • Filter popular articles by category:If you want to display popular articles under a specific category, you canarchiveListadd acategoryIdparameter. For example, to display the 5 most popular articles under category ID 1, you can write:{% archiveList archives with type="list" order="views desc" limit="5" categoryId="1" %}
  • Display more article information:In addition to the title and view count, you can also display the article summary as needed (item.Description) author, custom fields, etc.
  • Style adjustment:The code only provides the functionality implementation, you can use CSS to style.popular-articles/.article-title/.article-viewsthe class names to match the overall design style of your website.
  • combined with the latest articles:The popular article list will usually appear at the same time as the latest article list, both of which can be recommended as complementary content to users, using differentorderparameters (such as using the latest articles withorder="id desc"ororder="createdTime desc")

By using these flexible template tags provided by AnQiCMS, you can conveniently build dynamic and attractive content display modules, thereby effectively enhancing the user experience and operational effectiveness of the website.


Frequently Asked Questions (FAQ)

1. How will the sorting be handled in the popular articles list if multiple articles have the same number of views (Views)?Answer: When the views of multiple articles are the same, AnQiCMS will default to sorting in reverse order by article ID.This means that articles with larger IDs (usually the latest published articles) will be displayed first, reflecting the principle of 'relative updating'.

2. Can I sort articles based on other metrics besides views?Answer: Of course you can.archiveListlabel'sorderThe parameters are very flexible. For example, if you want to sort by the latest release time, you can useorder="id desc"ororder="createdTime desc"; if the backend has set a custom sorting field (such assort), you can also useorder="sort desc".

3. Do I need to write multiple similar codes to display a different number of popular articles at different locations on the website?Answer: Yes, whenever you need to display popular articles in different areas or in different quantities on the website, you need to use separatearchiveListtag blocks. EacharchiveListTags can be set independentlylimitParameters to control the display quantity, or through `