AnQi CMS is an efficient and customizable content management system that plays an important role in website operations.Its template system is powerful and flexible, allowing operators to finely control the display method of content according to actual needs.Today, let's delve into a common problem in actual operation that beginners may find confusing: how tocategoryListIn the tag, cleverly display the 'brother categories of the current category' rather than its subcategories.
It is of great significance to display the brother category list in terms of content organization and user experience.It can help users quickly find other related categories at the same level while browsing content in a category, thereby promoting content discovery, improving the quality of internal links on the website, and ultimately optimizing SEO performance.
Deep understandingcategoryListThe flexibility of the tag
Of Security CMScategoryListTags are the core tools you use to call classification data in templates. They provide rich parameters that allow you to accurately filter and display the classification information you need. Usually, we use them to:
- Get the top-level category: By setting
parentId="0", you can easily list all top-level categories that do not belong to any parent category. - Get the child categories under a specified parent category: By specifying a specific
parentId="某个分类ID"You can obtain all direct subcategories under this ID. - Get all categories: You can set to display the entire category tree when necessary
all=trueand combinemoduleIdto get all categories under a specific model.
However, the focus of what we need to pay attention to today is how to go beyond these common usages and achieve an accurate acquisition of 'brother category'.
To implement the list of 'brother categories' of the current category:parentId="parent"The mystery of
The designer of AnQi CMS clearly considered this operational scenario andcategoryListreserved a very concise and powerful parameter value in the tag:parentId="parent".
When you use on the category detail page (or any page that can identify the current category context)categoryListSet a tag,parentIdthe parameter to"parent"The system will automatically identify the parent category of the current category, and then list all categories that belong to the same parent category as the current category. This is what we call 'sibling categories'.
It is worth mentioning that this mechanism is very intelligent, as it saves the麻烦 of manually obtaining the parent ID of the current category, and then using the parent ID to query the child categories (excluding the current category itself).It directly completes this logic for you.
Let us demonstrate how to implement it with a specific example:
Suppose we are designing a named文章Under the model, we want to display other categories at the same level as "Product Introduction" in the sidebar, such as "Company News", "Industry Dynamics", etc.
{# 首先,确保我们能获取到当前分类的详细信息。
在分类详情页,通常系统会自动提供当前分类的上下文,
或者我们可以使用 categoryDetail 标签明确获取。 #}
{% categoryDetail currentCategory with name="Id" %}
{# currentCategory 变量现在存储了当前分类的完整数据,包括其ID、父ID等 #}
<div class="sibling-categories">
<h3>同级分类</h3>
<ul>
{# 使用 categoryList 标签,关键是设置 parentId="parent" #}
{# 并且为了确保只显示当前模型下的分类,最好加上 moduleId #}
{% categoryList siblings with moduleId=currentCategory.ModuleId parentId="parent" %}
{% for item in siblings %}
{# 排除当前正在浏览的分类自身 #}
{% if item.Id != currentCategory.Id %}
<li class="{% if item.IsCurrent %}active{% endif %}">
<a href="{{ item.Link }}" title="{{ item.Title }}">{{ item.Title }}</a>
</li>
{% endif %}
{% empty %}
<li>暂无其他同级分类</li>
{% endfor %}
{% endcategoryList %}
</ul>
</div>
In this code block:
- We first pass through
{% categoryDetail currentCategory with name="Id" %}(Here we only retrieve the ID as an example, actualcurrentCategorywill contain more fields) to ensure we have the classification context of the current page. Even on the classification list page,currentCategoryVariables may also be filled in automatically by the system. - Next, we call
{% categoryList siblings with moduleId=currentCategory.ModuleId parentId="parent" %}. Here,moduleId=currentCategory.ModuleIdEnsure that we only retrieve sibling categories that belong to the same content model (such as "articles" or "products") to avoid confusion.parentId="parent"This is the key instruction to implement the retrieval of sibling categories. - In
forInside the loop, we added a{% if item.Id != currentCategory.Id %}The condition judgment is to exclude the category being visited by the current user from the brother list to avoid duplication and thus provide clearer navigation. {% empty %}The label elegantly handles the situation when there are no other sibling categories to display, enhancing the user experience.
With such a simple configuration, you can dynamically display the list of sibling categories on any category page of the website, greatly enhancing the navigation capabilities and user-friendliness of the site.
Operations Tips
- Sidebar navigation: Place the sibling category list in the sidebar of the category page, which is an efficient way to guide users to discover more related content.
- Supplement to breadcrumb navigation: Although breadcrumbs mainly show the hierarchical relationship, the list of sibling categories provides horizontal expansion, and the two complement each other.
- Content relevance: Sibling categories usually mean a close relevance of content themes, which helps to enhance the correlation between pages and is very beneficial for the internal link structure of SEO.
The template tag design of Anqi CMS aims to simplify complex logic,parentId="parent"The usage is the ultimate embodiment of this concept. Master this skill, and your website operation will rise to a new level, and users can also navigate freely on your website.
Frequently Asked Questions (FAQ)
Q1: If the current category is a top-level category (i.e., it has no parent), useparentId="parent"what will be displayed?
A1: When the category you are currently in is itself a top-level category, it does not have the concept of a 'parent', and therefore does not have 'sibling categories'. In this case,parentId="parent"No results will be returned,categoryListin the loop{% empty %}Part will be executed, you can set the corresponding prompt information, such as “No同级category”or “You are currently at the top level category.”
Q2: Can I retrieve brother categories and subcategories at the same time and display them in the same area?
A2: Yes, but you need to call it twice separatelycategoryListLabel to get these two data. Use onceparentId="parent"Get the sibling category, and the other time it can be omittedparentIdParameters (if the current page is a category page, it will default to fetching the subcategories of the current category), or explicitly specify the category ID to fetch its subcategories.After that, you can merge them to display in the template, or place them separately in different visual areas.
Q3:categoryListin the labelmoduleIdIs the parameter required here?
A3: Although in some page contexts, the system may be able to make intelligent judgmentsmoduleIdHowever, for the robustness and clarity of the code, it is strongly recommended to usecategoryListWhen obtaining a specific type of classification (such as article classification, product classification), always specify it clearlymoduleIdParameters. This ensures that even in complex template structures, you can accurately obtain the classification data you expect, avoiding confusion between different content models. Usually,currentCategoryThe object will containModuleIdfields can be directly referenced.