As a senior website operations expert, I know how important it is to have precise control over the list content in content management.Especially in the context of increasingly complex website structures and increasingly rich content, how to flexibly display or hide specific content is directly related to user experience and operational efficiency.categoryListLabel whether it can exclude certain specific classification IDs and not display them in the list.
categoryListWith classification exclusion: insights from the document.
Firstly, let's directly answer the core question: Based on the AQ CMS document you provided, and in conjunction with my long-standing understanding of the system,categoryListthe tag itself并未直接提供a nameexcludeCategoryIdParameters or similar functions that allow you to exclude specific category IDs at once when calling.
After careful reviewtag-/anqiapi-category/151.htmlof the document, we can seecategoryListThe supported parameters of the label include:
moduleId:Used to get the category list under the specified content model.parentId:Used to get the subcategory list under the specified parent category, or the top-level category (parentId="0").all:Used to get all categories.limit:Controls the number of displayed items.siteIdIn a multi-site environment, specify site data.
In comparison, the document list is addressed.archiveListTags, the document explicitly mentions.excludeCategoryIdParameters can be used to exclude documents under certain categories when retrieving documents.This indicates that the system has adopted different strategies in the design of the classification list, and may be more inclined to further process the data after obtaining it through template logic.
Flexible solution: conditional judgment at the template level.
AlthoughcategoryListNo built-in exclusion parameters, but this does not mean that we cannot meet this requirement.The AnQi CMS powerful template engine is based on the Django template engine syntax of the Go language, providing flexible logical control capabilities.forLoop combinedifConditional judgmentTo implement the exclusion display for a specific category.
The core idea of this method is: first throughcategoryList
The following is a specific code example demonstrating how to elegantly exclude one or more specific category IDs in a template:
{# 假设我们要获取模型ID为1(如文章模型)下的所有顶级分类,并排除分类ID为 5 和 10 的分类 #}
{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
{% for item in categories %}
{# 方案一:排除单个分类ID #}
{# 如果分类ID不等于 5,则显示 #}
{% if item.Id != 5 %}
<li><a href="{{ item.Link }}">{{item.Title}} - (单ID排除示例)</a></li>
{% endif %}
{# 方案二:排除多个分类ID,推荐使用 `not (item.Id in [ID1, ID2, ...])` 操作符 #}
{# 或者使用逻辑 AND 操作符: {% if item.Id != 5 and item.Id != 10 %} #}
{% if not (item.Id in [5, 10, 15]) %}
<li><a href="{{ item.Link }}">{{item.Title}} - (多ID排除示例)</a></li>
{% endif %}
{# 方案三:更通用的做法,定义一个排除列表变量 #}
{% set exclude_ids = [20, 25] %} {# 可以在模板头部或全局定义 #}
{% if not (item.Id in exclude_ids) %}
<li><a href="{{ item.Link }}">{{item.Title}} - (变量列表排除示例)</a></li>
{% endif %}
{% endfor %}
</ul>
{% endcategoryList %}
In this code block:
- We first use
{% categoryList %}The tag has obtained the specifiedmoduleIdandparentIdThe data under all categories, and store the results incategoriesthe variable. - Then, we use
{% for item in categories %}Loop through this category list. - Inside the loop, we use
{% if %}tags for eachitem(i.e., each category) ofIdfor judgment.- If you only need to exclude one category,
item.Id != 5this condition is sufficient. - If you need to exclude multiple categories, you can use
not (item.Id in [5, 10, 15])This syntax checks whether the current category ID is in the exclusion ID list you defined. This method is concise and clear, and easy to expand. - In order to better manage and maintain, you can even define the list of IDs to be excluded as a variable, just like in Plan Three, making the template logic clearer.
- If you only need to exclude one category,
The advantages and disadvantages of this method
Advantages:
- Extremely flexible:You can perform complex exclusion logic based on any classification attribute (not just ID, but also name, description, etc.).
- No modification of core code required:All operations are completed at the template level, without affecting the backend logic of AnQiCMS, maintaining system stability.
- Immediate effect:Modify the template and the effect will usually be visible on the front end immediately (may require clearing the cache).
- Easy to understand and maintain:For operators familiar with template syntax, this logic is relatively intuitive.
Disadvantages:
- Performance consideration (mild):The system will first retrieve all data that matches from the database
categoryListThe classification of initial conditions, then filtering is performed in the template.If the number of categories is very large and you have many category IDs that need to be excluded, theoretically, it may be slightly less efficient than excluding them directly at the database level.But for most small and medium-sized enterprises and self-media websites, this performance overhead can be ignored almost.
Operation suggestions
In actual operation, when you encounter the need to exclude specific categories, the condition judgment at the template level mentioned above is undoubtedly the most direct and recommended method.It maximizes the flexibility of the front-end display, allowing content operators to quickly respond to changes and adjust the presentation of the website's classification structure.
Common Questions (FAQ)
Q1: Will this template-level exclusion method really affect the website's SEO indexing, leading to the excluded categories not being crawled by search engines?A1: Will not.This exclusion only controls the display of the category list on the front-end page, and does not affect whether the page of the excluded categories is generated and accessed.If these excluded category pages can still be accessed through other entry points (such as in-site search, category links on article detail pages, Sitemap, etc.), search engines can still crawl and index them.This operation is more about front-end layout and user experience considerations.
Q2: If I want to exclude the category ID dynamically, for example, based on user permissions or a specific event, can this be implemented in the template?A2: Can be implemented. Template logic supports dynamic variables. You can use dynamic variables in the template.{% set %}or other data passing mechanisms to pass the dynamically generated category ID list toexclude_idsVariable, then continue using{% if not (item.Id in exclude_ids) %}Make a judgment. For example, the backend can pre-process and pass an array containing user inaccessible category IDs to the template.
Q3: Will the future version of AnQi CMS consider?categoryListAdded inexcludeCategoryIdsuch parameters?A3: It cannot be directly known from the document what the future development plan of AnQi CMS will be. But consideringarchiveListAlready has similar functions,categoryListThe demand for flexibility is growing, which is indeed a feature point that is worth feeding back and suggesting to the developer community.In the current version, conditional judgment at the template level is still the most stable and efficient implementation method.