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

Calendar 👁️ 70

In website content management, we often encounter such needs: we hope to display articles under a specific category on a certain page, but we do not want to include the sub-category articles under that category, in order to maintain the purity and focus of the content.For example, you may have a "Company News" category that includes subcategories such as "Enterprise Dynamics" and "Industry Information", but on the homepage, you only want to display the pure "Company News" without mixing in the content of all subcategories.

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

Understanding Core Needs: Precise Content Location

Why is there such a need? It is usually for:

  1. Keep the topic focused:Ensure that the content displayed strictly conforms to the current category theme, and avoid the detailed content of subcategories from distracting from the main theme.
  2. Optimizing 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 primary category in a specific area, while placing more detailed category content on other pages.

The Anqi CMS solution: smart usearchiveListlabel'schildParameter

In AnQiCMS, to implement 'only display articles under a specific category without including its subcategories', we mainly rely on the template ofarchiveListthe tag and one key parameter:child.

archiveListThe tag is the core tool used by AnQiCMS to call the article list. It provides a rich set of 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 want to retrieve. You can find the corresponding category ID in the "Content Management" -> "Document Category" of the AnQiCMS backend.
  • childparameters:This is the core of implementing articles without subcategories. According to the AnQiCMS documentation,childThe default value of the parameter istrueThis means it will display articles of the specified category and all its subcategories. And when you explicitly set itfalsethen,archiveListLabels willOnlyTo get the specifiedcategoryIdarticles, excluding the content of subcategories.

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

Practical Operation: Three Steps to Achieve It Easily

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

First step: Determine the target category ID

First, you need to know the category ID you want to display the article in. Log in to the AnQiCMS backend, navigate to the left menu's "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).Note the ID of the category you are targeting, for example, suppose your "Company News" category ID is5.

Second step: Edit the template file

Next, we need to edit the template file on your website that needs to display these articles. AnQiCMS template files are usually located in/templatethe directory, and.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 (index/index.htmlorindex.html) or within a custom content block.

Step 3: InsertarchiveListtag code

After finding and opening the template file you wish to modify, you need to insertarchiveListLabel 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 of5of the category, the latest 10 articles, and do not include subcategories:

{# 假设我们只想显示分类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, it defines a namedarticlesto store the retrieved article list.
    • categoryId="5": Instructs the system to only retrieve articles from category 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"This indicates that a simple article list is being retrieved, not a paginated list (if pagination is needed, you can usetype="page"and combinepaginationtags).
    • limit="10": Limits to display the latest 10 articles.
  • {% for item in articles %}...{% endfor %}This is a loop structure used for iterationarticlesEach article stored in the variable.
  • {{ item.Link }}: Output the link of the article.
  • {{ item.Title }}The title of the article.
  • {{ stampToDate(item.CreatedTime, "2006-01-02") }}: Format and output the publication time of the article.stampToDateIs a convenient time formatting function provided by AnQiCMS.
  • {{ item.Description|truncatechars:100 }}The introduction of the article, and usetruncatecharsFilter the first 100 characters.
  • {% empty %}If:articlesThe 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

  • Test and verification:After the code modification is completed, it is necessary to test on the front-end page. Check whether the article indeed comes from the specified category and there are no articles from sub-categories.
  • Cache cleanup: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).
  • The classification structure is clear:Maintain a clear classification structure in the background to facilitate management and access. Regularly check the classification ID to ensure consistency with the settings in the template.

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


Frequently Asked Questions (FAQ)

1. How do I display articles under a certain category, including all subcategories of the article?

Answer: This isarchiveListthe default behavior of the tag. You just need to removechild=falseThis parameter, or set it explicitly.child=true.{% 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), and none of them contain 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 (excluding 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 setchild=falsebut it didn't work, or the number of articles on the page is incorrect?

Answer: In this situation, you can investigate from the following aspects:

  • Is the category ID correct?Please confirm again thatcategoryIdthe ID you entered is indeed the target category you want.
  • Did you spell the code incorrectly?Checkchild=falseAre there any spelling errors, for examplechlid=falseorchild="false"Although in most cases the AnQiCMS template engine will automatically convert boolean strings, it is clearer to writefalsemore standard).
  • Cache problem?Clear AnQiCMS backend cache and your browser cache to ensure that the latest template file is loaded.
  • Does the article exist?Confirm that there are published articles in your target category, and that these articles have not been set as drafts or hidden.

Related articles

How to exclude specific categories or multiple categories of articles from the document list?

When managing website content in Anqi CMS, we often need to precisely control the display of articles.Sometimes, we may want certain category articles not to appear in the regular article list, such as for internal notifications, test content, or some promotional information that is only displayed on specific pages.AnQi CMS provides a simple and efficient method to meet this kind of need, allowing you to flexibly exclude articles of specific categories or multiple categories, thereby achieving more accurate content presentation.

2025-11-08

How to filter and loop through the document list under a specific category based on the category ID?

It is crucial to organize and present content when building and operating a website.Especially when the content of a website becomes increasingly rich, how to enable visitors to quickly find the information they are interested in and clearly browse all articles under a specific theme has become a carefully designed problem.AnQiCMS (AnQiCMS) provides powerful and flexible content management capabilities, allowing us to easily achieve precise control and display of document lists.

2025-11-08

How to display the latest N articles or products on the homepage and implement pagination control?

## Asecurity CMS: The Practice of Efficiently Displaying the Latest Content on the Homepage and Implementing Pagination The homepage is an important entry point for visitors to understand the site content and obtain the latest information, it is crucial to clearly and effectively display the latest published articles or products.The AnQi CMS provides powerful and flexible template tags, which help us easily achieve this goal, and can also fine-tune content pagination to ensure a smooth user experience.

2025-11-08

How to output variables in a template and safely escape content to prevent XSS attacks?

In the process of managing and displaying website content, ensuring the security of user data is a crucial link.It is an issue that every website operator needs to pay attention to when the website needs to display user submitted content or data obtained from external sources, how to effectively prevent cross-site scripting (XSS) attacks.AnQiCMS (AnQiCMS) took this into full consideration from the very beginning, providing solid security guarantees for content output through its powerful template engine and flexible filter mechanism.### Cross-Site Scripting Attack

2025-11-08

How to filter and display a list of articles based on their recommended attributes (such as “Top Article”, “Recommended”)?

In Anqi CMS, efficient content management is one of the keys to website success.Article recommendation attribute, as an important link in the operation of content refinement, it can help us display website content more flexibly, guide users to pay attention to key information, and thus improve the activity and conversion rate of the website.This article will delve into how to filter and display article lists based on the recommended attributes of articles (such as "Headline

2025-11-08

How to display the article list according to the article views, publish time, or backend custom sorting?

In website operation, the way articles are displayed directly affects user experience and content access efficiency.A well-organized list of articles that can highlight key points, significantly improving user retention and information acquisition efficiency.AnQiCMS as a flexible content management system provides powerful article list display functions, not only can it flexibly filter content, but also can freely adjust the sorting method of articles according to your content strategy, making your website content more attractive.

2025-11-08

How to obtain and display the title, content, thumbnail, and other core information of an article on the article detail page?

When operating a website, the article detail page is the key entry point for users to understand the core value of the content.A well-designed, comprehensive article detail page that not only improves user experience but also effectively helps search engines understand the content of the page, thus optimizing the website's SEO performance.AnQiCMS (AnQiCMS) provides a powerful and flexible template tag system that allows you to easily obtain and present all the core information of the article.

2025-11-08

How to retrieve the custom fields (such as author, source, additional parameters) of the current article and display them?

In Anqi CMS, managing and displaying custom fields for articles such as authors, sources, or additional parameters is a common and practical need in content operation.The Anqi CMS, with its flexible content model design, provides multiple ways to achieve this goal, helping you easily enrich article information and present it to readers in a personalized manner. ### Understanding AnQi CMS Custom Fields Firstly, we need to understand how article custom fields work in AnQi CMS.

2025-11-08