When managing website content in Anqi CMS, we usually organize articles into different categories, forming a clear hierarchical structure.This structure provides great convenience when managing content in the background.However, when displaying the article list on the front-end page, we sometimes need to control the display range of the content more precisely, for example, we only want to display the articles directly published under the current category, and not include the content in the sub-categories.

Fortunately, AnQi CMS provides a simple and powerful solution: utilizingarchiveListtemplate tagschildParameter.

Understanding the problem: the need for default behavior and precise control

By default, when you are on a category page (for example, visiting the "News Center" category) usingarchiveListthe tag to call the article list, Anqi CMS will intelligently obtain the articles under the category ofand all its subcategoriesarticles. This is convenient in many scenarios because it can automatically aggregate related content.

But imagine such a situation: you have a "product introduction" category, under which there are subcategories such as "mobile phones", "computers", and "accessories."}On the "Product Introduction" page, you might only want to display some general, overview articles that belong directly to the "Product Introduction" category, rather than mixing all specific product articles under "Mobile", "Computer", and "Accessories" together.At this point, it is necessary to exclude articles from subcategories.

Solution:archiveListlabel'schild=falseParameter

Of Security CMSarchiveListTags are the core tools used to retrieve and display article lists in templates. They provide multiple parameters to finely control the filtering conditions of articles, among whichchildThe parameter is the key to solving our current problem.

childThe parameter supportstrueorfalsetwo values:

  • child=true(Default behavior): This will retrieve the articles under the specified category and all its subcategories. If you do not set this parameter, the system will default totrueProcess.
  • child=falseThis will only retrieve the specified category belowDirect publishof the article, completely excluding the content of any subcategories.

Therefore, to implement a document list that only displays documents under the current category and not the content of its subcategories, we need toarchiveListExplicitly specify in the tagcategoryIdthe parameter (i.e., the ID of the current category), and tochildthe parameter tofalse.

Implementation steps and code examples

Typically, you will be in a category list template file (such as/template/你的模板名/article/list.htmlor more specificarticle/list-{分类ID}.htmlUsed inarchiveListtags to display the article.

  1. Get the current category ID:In the context of the category page, Anqicms usually exposes the current category information to the template. You can{{category.Id}}to obtain the unique ID of the current category.

  2. UsearchiveListTags:Assign the obtained category ID toarchiveListofcategoryIdParameters, and you willchildthe parameter tofalse.

Below is a specific template code example showing how to display only the current category's articles on the article list page, without including the content of subcategories:

{# 假设这是在“文章”模型的某个分类列表模板中(例如:/template/default/article/list.html) #}

{# 1. 获取当前分类的ID。在分类页面上下文中,可以直接访问 'category.Id' #}
{% set currentCategoryId = category.Id %}

<div class="category-articles-section">
    {# 2. 显示当前分类的标题 #}
    <h1>{{ category.Title }} 下的最新文章</h1>
    <p>(只包含直接发布在本分类下的内容,不含子分类)</p>

    <ul>
        {# 3. 使用 archiveList 标签,指定当前分类ID,并设置 child=false #}
        {% archiveList archives with type="page" categoryId=currentCategoryId child=false limit="10" %}
            {% for item in archives %}
            <li>
                <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
                <p>{{ item.Description }}</p>
                <small>发布日期: {{ stampToDate(item.CreatedTime, "2006-01-02") }} | 浏览量: {{ item.Views }}</small>
            </li>
            {% empty %}
            <li>
                当前分类下没有直接发布的文章。
            </li>
            {% endfor %}
        {% endarchiveList %}
    </ul>

    {# 4. 如果需要分页,可以继续添加分页标签 #}
    <div class="pagination-area">
        {% pagination pages with show="5" %}
            {# 首页 #}
            <a class="{% if pages.FirstPage.IsCurrent %}active{% endif %}" href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a>
            {# 上一页 #}
            {% if pages.PrevPage %}
            <a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a>
            {% endif %}
            {# 中间多页 #}
            {% for item in pages.Pages %}
            <a class="{% if item.IsCurrent %}active{% endif %}" href="{{item.Link}}">{{item.Name}}</a>
            {% endfor %}
            {# 下一页 #}
            {% if pages.NextPage %}
            <a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a>
            {% endif %}
            {# 尾页 #}
            <a class="{% if pages.LastPage.IsCurrent %}active{% endif %}" href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a>
        {% endpagination %}
    </div>
</div>

By the above code, you can precisely control the article list to only display the content under the current category without worrying about the mixing of articles in subcategories.This approach helps create a clearer and more user expectations conforming content display page, improving the overall website user experience and content management efficiency.


Frequently Asked Questions (FAQ)

Q1: Why did my article list show subcategory content even though I did not explicitly set itchildthe parameter?A1:archiveListin the labelchildThe default parameter value istrue. This means if you do not explicitly setchild=falseThe system will automatically include the current category and all subcategories under it. To only display the content of the current category, be sure tochildthe parameter tofalse.

Q2: How to dynamically assign the ID of the current category in the template tocategoryIdthe parameter?A2: When handling the template of the category list page or category detail page, the information of the current category is usually loaded intocategorythe object. Therefore, you can use it directly.{{category.Id}}Get the current category ID and assign it toarchiveListlabel'scategoryIdParameter.

Q3: How should I set up to display all categories (including subcategories) articleschildthe parameter?A3: If you wantarchiveListThe label behavior is set to default, which means it displays articles of the current category and all its subcategories, you can choose not to set itchildparameter (because it is set totrue), or explicitly set it tochild=true. Both methods have the same effect. For example:{% archiveList archives with type="page" categoryId=currentCategoryId limit="10" %}or{% archiveList archives with type="page" categoryId=currentCategoryId child=true limit="10" %}.