In a content management system, effectively organizing and displaying content is the key to enhancing user experience and website SEO performance.English CMS with its flexible and powerful template engine allows you to easily implement the requirement of retrieving and displaying an article list based on a specific category ID.Whether it is to build a special page, classify archives, 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 use the template tag feature of the Anqi CMS, especiallyarchiveListtags, to accurately achieve this goal.


1. Preliminary preparation: Understand the category ID

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


English, Core Functions:archiveListThe magic use of tags

The template system of Anqi CMS uses syntax similar to Django,archiveListThe tag is a core tool for retrieving article lists. It provides rich parameters, allowing you to highly customize the filtering and display of content.

1.archiveListBasic structure of tags

archiveListTags are usually withforLoop combination usage, used for traversing the obtained list of articles. Its 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,变量名It is customized by you, used for storing the obtained list of articles (for example,articles/newsListetc.).itemThen it is a temporary variable for each article in the loop.

2. Detailed Explanation of Key Parameters

To accurately obtain articles based on the category ID, we need to pay special attention to the following parameters:

  • categoryIdThis is the core parameter for "Accurate retrieval based on category ID". You just 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 articles in the current category page (for example, you are visiting a category list page), you can specify it.categoryId,The system will automatically read the current category ID; if you want to explicitly not read automatically and only filter through other parameters, you can set it tocategoryId="0".
  • moduleIdIn the Auto CMS, content usually belongs to different 'models' (such as article models, product models). To ensure that you are getting an article, it is usually necessary to specifymoduleId="1"(Article model's default ID)。If you are not sure, you can check the 'Article Model' ID in the 'Content Model' management on the backend.
  • limit: Control the number of articles you want to display. For example,limit="10"The display will show 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, likelimit="2,5"English translation: (Represents starting from the element at index 2 and getting 5 elements).
  • orderEnglish translation: : Defines the sorting method of the article. Common ones include:
    • order="id desc"English translation: : Sorts by article ID in descending order (the most recently published articles come first).
    • order="views desc":按浏览量降序(热门文章在前)。
    • order="CreatedTime desc":按创建时间降序(与)id desc类似,更明确)。
  • type: 决定列表的类型。
    • type="list"(默认值): 获取指定数量的文章列表,不带分页。
    • type="page": 用于需要分页显示的场景,需要与paginationauto tag to use.
  • child: (默认为trueIf you specify a category ID and want to include all subcategories under that category, leave the default value.child=trueyou can get it.the current specified category itselfof the article, but not including the articles of its subcategories, you need to set it explicitly,child=false.
  • excludeCategoryIdIf you want to get articles under a certain model but wish to exclude certain specific categories, you can use this parameter. For example,excludeCategoryId="2,4"The articles under the categories with IDs 2 and 4 will be excluded.

三、Practice Scenario: Displaying Article List Based on Category ID

Let's see how to display the article list according to different needs through some actual code examples.

1. Show the latest article list of a single category.

Suppose 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>

Description:

  • 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.