In the process of managing website content in AnQi CMS, we usually organize articles according to different categories to form 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 accurately, for example, we only want to display the articles directly published under the current category, but not include the content from its subcategories.

幸运的是,安企CMS为此提供了一个简单而强大的解决方案:利用 EnglisharchiveListtemplate tags.childParameter.

理解问题:默认行为与精确控制的需求 English

By default, when you are on a category page (such as, visiting the "News Center" category) usingarchiveListthe tag to call the article list, the Safe CMS will intelligently obtain the articles under the categoryAll its subcategoriesThe articles under. This is convenient in many scenarios, as it can automatically aggregate related content.

But imagine such a situation: you have a "Product IntroductionOn the 'Product Introduction' page, you may only want to display some general, overview articles that directly belong to the 'Product Introduction' category, rather than mixing all specific product articles under 'Mobile', 'Computer', and 'Accessories' together.This is when you need to exclude articles from subcategories.

Solution:archiveListTagschild=falseParameters

Anqi CMS'sarchiveListLabels are the core tools used to retrieve and display article lists in templates. They provide multiple parameters for fine-grained control of the article filtering conditions, wherechildThe parameter is the key to solving our current problem.

childThe parameter supportstrueorfalsetwo values:

  • child=true(Default behavior):This will retrieve articles under the specified category and all its subcategories. If you do not set this parameter, the system will default totrueProcessing.
  • child=false:This will only retrieve articles under the specified categoryPublish directlyThe article, completely excluding its content from any subcategory.

Therefore, to display the article list that only shows the documents under the current category and not including the content of its subcategories, we need to inarchiveListspecify in the tag.categoryId参数(即当前分类的ID),并将其childparameter settingsfalse.

实施步骤与代码示例

通常,你会在一个分类的列表模板文件(例如/template/你的模板名/article/list.htmlor more specificallyarticle/list-{分类ID}.html) usedarchiveList标签来展示文章。

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

  2. UsearchiveListTags:Assign the obtained category ID toarchiveListofcategoryIdthe parameter, andchildparameter settingsfalse.

Below is a specific template code example showing how to display only the articles of the current category on the article list page, without including the content of the 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 using the above code, you can precisely control the article list to display only the content under the current category, without worrying about the mixing of subcategory articles.This approach helps to create a clearer, more user-expectant content display page, enhancing the overall website user experience and content management efficiency.


Common Questions (FAQ)

Q1: Why did my article list show subcategory content even though I did not explicitly set itchild参数?A1:archiveListthe tag inchildThe default value of the parameter istrue. This means that if you do not explicitly setchild=false,The system will automatically include articles under the current category and all its subcategories. To only display the content of the current category, be sure to setchildparameter settingsfalse.

Q2: In the template, how to dynamically assign the ID of the current category tocategoryId参数?A2: When processing the template of the category list page or category detail page, usually all information of the current category will be loaded intocategorythe object. Therefore, you can directly use{{category.Id}}Come get the current category ID and assign it toarchiveListTagscategoryIdParameter.

Q3: How should I set it to display all articles of categories (including subcategories)?child参数?A3: If you want toarchiveListThe behavior of the label remains default, i.e., it displays articles of the current category and all its subcategories at the same time, you can choose not to set itchildparameter (because it is default astrue), or explicitly set it tochild=trueTwo 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" %}.