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:
- 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.
- Optimizing user experience: Help users quickly find the specific content they are interested in, reducing information overload.
- Simplify the page structure:Make the page layout clearer, content hierarchy more distinct, convenient for users to browse and understand.
- 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 that
categoryIdthe ID you entered is indeed the target category you want. - Did you spell the code incorrectly?Check
child=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.