In a content management system, effectively organizing and displaying content is the key to improving user experience and website SEO performance.AnQi CMS with its flexible and powerful template engine, allows you to easily implement the need to retrieve and display a list of articles based on a specific category ID.Whether it is to build a special page, category archive, or simply to display the latest content of a specific column on the homepage, Anqi CMS can provide an intuitive and efficient solution.

This article will delve into how to utilize the template tag function of Anqi CMS, especiallyarchiveListtags, to achieve this goal accurately.


1. Preliminary preparation: Understand the category ID

Before starting to write the template code, we first need to clarify the ID of the target category.In the AnQi CMS backend, find the 'Document Classification' option under the 'Content Management' menu.Here, you will see a list of all categories created. Each category will have a unique numeric ID (usually visible in the URL or on the detail page when editing).For example, the ID of your 'Company News' category may be '1', and the ID of the 'Product Introduction' category may be '5'.Please note the category ID you need to retrieve the article list, which will be the key for us to call accurately in the template.


Second, core functions:archiveListThe clever use of tags

The template system of Anqi CMS adopts syntax similar to Django, wherearchiveListTags are the core tools for obtaining article lists. They provide rich parameters, allowing you to highly customize the filtering and display of content.

1.archiveListBasic structure of the tag

archiveListTags are usually withforCombine loops to traverse the list of obtained articles. The basic syntax is as follows:

{% archiveList 变量名 with 参数1="值1" 参数2="值2" %}
    {% for item in 变量名 %}
        {# 在这里展示每篇文章的信息 #}
        <a href="{{ item.Link }}">{{ item.Title }}</a>
        {# ... 更多文章详情字段 ... #}
    {% empty %}
        {# 如果没有找到文章,显示此内容 #}
        <p>该分类下暂无文章。</p>
    {% endfor %}
{% endarchiveList %}

Among them,变量名Is customized by you, used to store the obtained list of articles (for examplearticles/newsListetc.).itemIt is a temporary variable for each article in the loop.

2. Detailed explanation of key parameters

In order to accurately obtain articles according to the category ID, we need to pay attention to the following parameters:

  • categoryIdThis is the core parameter for implementing "accurate retrieval according to category ID." You only need to assign the ID of the target category to it. If there are multiple categories, you can use a comma to separate them.,Separate multiple IDs, for examplecategoryId="1,3,5". If you want to list the articles in the current category page (for example, if you are visiting a category list page), you can specify it withoutcategoryIdThe system will automatically read the current category ID; if you want to explicitly not read it and only filter through other parameters, you can set it tocategoryId="0".
  • moduleIdIn AnQi CMS, content is usually attributed to different 'models' (such as article models, product models). To ensure that you are getting an article, it is usually necessary to specifymoduleId="1"(The default ID of the article model). If you are not sure, you can check the ID of the article model in the 'Content Model' management on the backend.
  • limit: Control the number of articles you want to display. For example,limit="10"It will display 10 articles. If you need to skip the first few articles to get the subsequent articles (such as implementing "start displaying 5 articles from the 3rd article"), you can use a comma-separated format, such aslimit="2,5"This indicates that starting from the element at index 2, 5 elements are to be obtained.
  • order: Defines the sorting method of the articles. Common ones include:
    • order="id desc": Sorts by article ID in descending order (the most recently published articles are at the top).
    • order="views desc": Sort by view count in descending order (popular articles first).
    • order="CreatedTime desc": Sort by creation time in descending order (withid descsimilar, more explicit).
  • type: Determines the type of list.
    • type="list"(Default): Get a list of specified number of articles without pagination.
    • type="page": Used for scenarios requiring pagination display, it needs to bepaginationtag usage
  • child: (Default istrue) If you specify a category ID and want to include all articles under the subcategories of that category, keep the default valuechild=truethen. If you only want to getthe current specified category itselfof the article, excluding the articles of its subcategories, then it needs to be explicitly set tochild=false.
  • excludeCategoryId: If you want to get articles under a certain model but want to exclude some specific categories, you can use this parameter. For exampleexcludeCategoryId="2,4"Articles under categories with IDs 2 and 4 will be excluded.

Chapter 3: Practical Scenario: Displaying Article Lists Based on Category ID

Let's look at some actual code examples to see how to display article lists based on different requirements.

1. Show the latest article list of a single category

Assume you want to display the latest 5 articles of "Company News" (category ID 1) in a certain area of the website.

<section class="latest-news">
    <h2>公司新闻</h2>
    <ul>
        {% archiveList news with categoryId="1" moduleId="1" limit="5" order="id desc" %}
            {% for item in news %}
                <li>
                    <a href="{{ item.Link }}" title="{{ item.Title }}">{{ item.Title }}</a>
                    <time datetime="{{ stampToDate(item.CreatedTime, "2006-01-02") }}">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</time>
                </li>
            {% empty %}
                <li>暂无公司新闻。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</section>

Explanation:

  • categoryId="1": Exactly specified the category ID as 1.
  • moduleId="1": Ensure that we are getting the article.
  • limit="5":Limit to displaying only 5 articles.