How to display articles under a specific category without including articles from its subcategories?

In website content management, we often encounter such a need: we want to display articles under a specific category on a page, but we do not want to include the articles under the subcategory of that category, in order to maintain the purity and focus of the content.For example, you might have a "Company News

AnQi CMS, as a powerful and flexible content management system, provides a simple and efficient way to meet the needs of such refined content display.This article will delve into how to achieve this goal in AnQiCMS.

Understand core needs: Precise content location

Why is there such a requirement? Usually, it is for:

  1. Keep the theme focused:Ensure that the content displayed strictly conforms to the theme of the current category, avoiding the scattering of detailed content in subcategories from the main theme.
  2. Optimize user experience:Help users quickly find the specific content they are interested in, reducing information overload.
  3. Simplify the page structure:Make the page layout clearer, content hierarchy more distinct, convenient for users to browse and understand.
  4. Support specific marketing strategies:For example, only display the latest updates of a specific first-level category in a specific area, while placing more detailed category content on other pages.

The solution of Anqi CMS: skillfully usearchiveListTagschildParameters

In AnQiCMS, to achieve 'only show articles under a certain category without including its subcategories', we mainly rely on the template in thearchiveListtags and one key parameter:child.

archiveListTags are the core tools used by AnQiCMS to call article lists. They provide rich parameters to control the filtering, sorting, and display of articles. Among them, childThe parameter is the key to solving this problem.

  • categoryIdParameters:This parameter is used to specify the category ID of the article you wish to retrieve. You can find the corresponding category ID in the 'Content Management' -> 'Document Category' section of the AnQiCMS backend.
  • childParameters:This is the core of implementing articles without subcategories. According to the documentation of AnQiCMS,childThe default value of the parameter istruemeans that it will display articles of the specified category and all its subcategories. While you explicitly set it tofalsewhenarchiveListtags toonlyto get articlescategoryIdunder, completely excluding the content of its subcategories.

Combine these two parameters, and we can accurately control the display range of the article.

Practical Operation: Three Steps to Easy Implementation

Now, let's see step by step how to operate specifically.

First step: Determine the target category ID

First, you need to know the category ID of the article you want to display.Enter AnQiCMS backend, navigate to the left menu "Content Management" -> "Document Category".In the category list, you can find the corresponding ID for each category (usually in the first column of the table or by viewing the URL when editing the category).5.

Second step: Edit the template file

Next, we need to edit the template file in your website that is used to display these articles. AnQiCMS template files are usually located/templatedirectory, and named with.htmlas a suffix. You can edit the template online through the 'Template Design' feature in the background, or directly modify the files on the server using FTP/SFTP tools.

For example, you may want to display these articles on the homepage of the website ()index/index.htmlorindex.html) or within a custom content block.

Step 3: InsertarchiveListtag code

After finding and opening the template file you want to modify, you need to insertarchiveListtag code. Here, we will usecategoryIdparameters to specify the category, and setchild=falseto exclude subcategory articles.

The following is a specific code example, assuming we want to display the ID of5category, the latest 10 articles are displayed, and subcategories are not included:

{# 假设我们只想显示分类ID为5(例如“公司新闻”)下的文章,不包含其子分类 #}
<div class="news-list-section">
    <h3>最新公司新闻</h3>
    <ul>
        {# 使用 archiveList 标签调用文章列表 #}
        {# categoryId 指定分类ID,child=false 确保不包含子分类文章 #}
        {# type="list" 表示以列表形式获取,limit="10" 限制显示10篇文章 #}
        {% archiveList articles with categoryId="5" child=false type="list" limit="10" %}
            {% for item in articles %}
            <li>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    {# 显示文章标题 #}
                    <h4>{{ item.Title }}</h4>
                    {# 显示文章发布时间,使用 stampToDate 格式化时间戳 #}
                    <span class="publish-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    {# 如果文章有缩略图,可以显示 #}
                    {% if item.Thumb %}
                        <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                    {% endif %}
                    {# 显示文章简介 #}
                    <p>{{ item.Description|truncatechars:100 }}</p>
                </a>
            </li>
            {% empty %}
            {# 如果分类下没有文章,显示此消息 #}
            <li>目前该分类下没有可展示的文章。</li>
            {% endfor %}
        {% endarchiveList %}
    </ul>
</div>

Code Explanation:

  • {% archiveList articles with categoryId="5" child=false type="list" limit="10" %}: This is the core tag, which defines a namedarticlesUse a variable to store the article list obtained.
    • categoryId="5"Tells the system to fetch articles only from the category with ID 5.
    • child=false:This is the key!It explicitly indicates that the system should not retrieve articles from any subcategory with ID 5.
    • type="list"[en] : indicates that a simple article list is being retrieved, not a paginated list (if pagination is needed, you can usetype="page"and combinepaginationTag).
    • limit="10"[en] : limits the display to the latest 10 articles.
  • {% for item in articles %}{% endfor %}This is a loop structure used for iteration.articlesEach article stored in the variable.
  • {{ item.Link }}Output the link of the article.
  • {{ item.Title }}Output the title of the article.
  • {{ stampToDate(item.CreatedTime, "2006-01-02") }}Output the formatted publish time of the article.stampToDateThis is a convenient time formatting function provided by AnQiCMS.
  • {{ item.Description|truncatechars:100 }}: Output the summary of the article, and usetruncatecharsThe filter to extract the first 100 characters.
  • {% empty %}IfarticlesThe list is empty (i.e., there are no articles under this category), and it will display{% empty %}and{% endfor %}the content between

some practical suggestions

  • Testing and verification:After the code modification is completed, be sure to test the front-end page. Check if the article is indeed from the specified category, and that no subcategory articles appear.
  • Cache cleaning:If the front-end does not take effect immediately after modifying the template, try to clear the AnQiCMS system cache (usually there is an option for 'Update Cache' in the background).
  • Clear category structure:Maintaining the clear category structure in the background aids in management and usage. Regularly check the category ID to ensure consistency with the settings in the template.

By following these steps, you can precisely control the display of articles in AnQiCMS, showing only the specific category content you wish, thereby enhancing the information architecture and user experience of the website.This flexibility of AnQiCMS makes content operation more efficient and accurate.


Common Questions (FAQ)

I want to display articles under a certain category, including all subcategories of that category. How should I do that?

Answer: This is actuallyarchiveListthe default behavior of the tag. You just need to removechild=falseThis parameter, or set it explicitly.child=trueIt is. For example:{% archiveList articles with categoryId="5" type="list" limit="10" %}This will retrieve articles under category ID 5 and all its subcategories.

How to display multiple specific category articles without subcategories on the same page?

Answer: If you want to display multiple top-level categories (such as company news, product updates) without their respective subcategories, you cancategoryIdParameters use commas to separate multiple category IDs and continue to usechild=falseFor example, to display articles with category IDs 5 and 8 (without their respective subcategories):{% archiveList articles with categoryId="5,8" child=false type="list" limit="20" %}This will retrieve a total of 20 articles from these two categories.

3. Why did I set itchild=falsebut it didn't take effect, or the number of articles on the page is incorrect?

Answer: You can check the following aspects for this situation:

  • Is the category ID correct?Please reconfirm that you havecategoryIdfilled in the ID you want to target.
  • Code spelling error?Checkchild=falseAre there any spelling errors, such aschlid=falseorchild="false"(Although most of the time, AnQiCMS's template engine will automatically convert boolean strings, it is clear that)falsemore standard).
  • Cache issue?Clear the AnQiCMS background cache and your browser cache to ensure that you are loading the latest template file.
  • Does the article exist?Confirm that there are indeed published articles under your target category, and these articles have not been set as drafts or hidden.