As an experienced website operations expert, I know how important it is to have fine control over the list content in content management.Especially in the context of increasingly complex website structures and rich content, how to flexibly display or hide specific content is directly related to user experience and operational efficiency.Today, we will delve into a common but cleverly handled requirement in AnQiCMS (AnQiCMS) -categoryListCan the tag exclude certain specific category IDs so that they do not display in the list.
categoryListWith category exclusion: insights from the document.
Firstly, let's directly answer the core question: based on the AQ CMS document you provided, combined with my long-term understanding of the system,categoryListThe tag itselfNot provided directlya namedexcludeCategoryIdParameters with similar functions, allowing you to exclude specific category IDs in one call.
After careful reviewtag-/anqiapi-category/151.htmlWe can see after the document.categoryListThe supported parameters of the tag include:
moduleId: Used to retrieve the category list under the specified content model.parentId: Used to retrieve the child category list under the specified parent category, or the top-level category (parentId="0")all: Used to retrieve all categories.limit: Controls the number of items displayed.siteIdSpecify site data in a multi-site environment.
In comparison, it is aimed at the document list.archiveListLabel, it is explicitly mentioned in the document.excludeCategoryIdThe parameter can be used to exclude documents from 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 after obtaining the data through template logic.
Flexible solution: conditional judgment at the template level.
ThoughcategoryListThere are no built-in exclusion parameters, but this does not mean that we cannot meet this requirement.The powerful template engine of AnQi CMS is based on the Django template engine syntax in Go language, providing flexible logic control capabilities.We can completely do it at the template level, byforLoop combinationifConditional judgmentTo implement the exclusion display for a specific category.
The core idea of this method is to first through.categoryListThe tag retrieves the list of all categories that meet the basic conditions (for example, all top-level categories under a model or all child categories under a parent category), and then performs ID judgment for each category item in the list.If the current category ID is one we want to exclude, skip and do not display;Otherwise, it will display normally.
Here 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 fetched the specifiedmoduleIdandparentIdAll category data under and store the result incategoriesthe variable. - Next, we go through
{% for item in categories %}Loop through this category list. - Inside the loop, we use
{% if %}Tags for eachitem(that is, each category) ofIdto judge.- If you only need to exclude one category,
item.Id != 5such conditions are enough. - If you need to exclude multiple categories, you can use
not (item.Id in [5, 10, 15])This syntax checks if the current category ID is in the exclusion ID list you defined. This method is concise 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
Advantage:
- 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.
- Instantly effective:After modifying the template, you will usually see the effect on the front-end immediately (you may need to clear the cache).
- Easy to understand and maintain:For operators familiar with template syntax, this logic is relatively intuitive.
Shortcomings:
- Performance consideration (mild):The system will first retrieve all items that meet the criteria from the database.
categoryListCategorization based on initial conditions, then filtering is performed in the template.If the number of categories is very large and you need to exclude many category IDs, theoretically, it may be slightly less efficient than excluding directly at the database level.But for most small and medium-sized enterprises and self-media websites, this performance overhead can be virtually ignored.
Operation suggestions
In actual operation, when you encounter the need to exclude specific categories, the conditional judgment at the template level is undoubtedly the most direct and recommended method.It maximizes the flexibility of the front-end display, allowing content operators to respond quickly to changes and adjust the presentation of the website's category structure.
Frequently Asked Questions (FAQ)
Q1: Will this template-level exclusion method really affect the website's SEO inclusion, causing excluded categories to be unable to be crawled by search engines?A1: I will not. This exclusion only controls whether the category list is displayed on the front-end page or not, and does not affect whether the page of the excluded category itself is generated and accessed.As long as these excluded category pages can 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 for front-end layout and user experience considerations.
Q2: If I want to exclude the category ID dynamically, such as based on user permissions or a specific event to hide, can it be implemented in the template?A2: Can be implemented. The template logic supports dynamic variables. You can pass dynamic category ID lists through the template by{% set %}or other data transmission mechanisms, to theexclude_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 the user's unauthorized access category IDs to the template.
Q3: Will AnQi CMS consider adding in the futurecategoryListaddexcludeCategoryIdsuch parameters?A3: It is not possible to directly know AnQi CMS's future development plan from the document. But consideringarchiveListAlready has a similar function, andcategoryListThe flexibility requirements are growing increasingly, this is indeed a feature point worth feeding back and suggesting to the developer community.In the current version, conditional judgment at the template level is still the most reliable and efficient implementation method.