As an experienced website operations expert, I am well aware that the flexibility of the Content Management System (CMS) is crucial for efficient operations.AnQiCMS (AnQiCMS) performs exceptionally well in this aspect with its powerful content model features.Today, let's delve deeply into a common requirement: how to utilizecategoryListLabels, precisely displaying only the categories under specific content models (such as articles or products).
In AnQi CMS, each content has its corresponding model, for example, the news articles on your website may belong to the 'Article Model', while the items for sale will be categorized into the 'Product Model'.This design makes content management orderly and well-organized.categoryListLabels, the powerful tool that you use to retrieve and display these category information in front-end templates.
Understand the content model and category association of Anqi CMS
In AnQi CMS, the content model is like the blueprint for different types of content on your website.It defines which fields, how to organize and display specific types of content (such as articles, products, news, cases, etc.) should include.For example, the 'article model' may include title, author, publish date, etc., while the 'product model' may contain product name, price, inventory, image set, etc.
Each category is closely associated with a specific content model.This means that when you create a category, you need to specify which content model it belongs to.This design logic ensures the specificity of classified content, for example, the "Company News" category must belong to the "Article Model", and the "Mobile Accessories" category belongs to the "Product Model".
It is this close relationship between the model and category that allows us to identify the content model (i.e.moduleId),to accurately filter out the category list we want.
categoryListThe core function of the tag andmoduleIdusefulness
categoryListThe tag is a key tool in the Anqi CMS template used to obtain the category list. It provides various parameters to help you flexibly control the displayed categories, and the most core, most able to meet our discussion needs today, is the parameter,moduleId.
moduleIdThe parameter allows you to specify the category list you want to retrieveContent model IDIn the Anqi CMS backend, each content model has a unique numeric ID. Usually, the "article model"moduleIdDefault to1while the "product model" ismoduleIdDefault to2. If you create a custom content model, they will also have corresponding IDs. You can view the ID of each model in the 'Content Management' -> 'Content Model' section of the backend.
By adding incategoryListSet in the labelmoduleIdParameters, you can easily filter out categories that belong only to specific content models, avoiding mixing different types of categories together, thus making your website structure clearer and content display more accurate.
Practice exercise: Step by step to build a classification list of specific models
Now, let's learn how to apply by using specific template code examplesmoduleIdParameter.
Example 1: Get all top-level categories under the article model
Assume you want to display only all top-level categories related to articles (such as "Company News", "Industry Dynamics", etc.) in the navigation bar or some sidebar, and these categories all belong to the "Article Model".}
You can use the following code:
{% categoryList categories with moduleId="1" parentId="0" %}
<ul>
{% for item in categories %}
<li>
<a href="{{ item.Link }}">{{ item.Title }}</a>
{# 这里可以根据需要,进一步展示子分类或该分类下的文档数量等信息 #}
<span>({{ item.ArchiveCount }}篇文章)</span>
</li>
{% else %}
<li>暂无文章分类。</li>
{% endfor %}
</ul>
{% endcategoryList %}
In this code block:
moduleId="1"Make it clearcategoryListTags, we only care about the categories under the content model with ID 1 (i.e., the article model).parentId="0"This indicates that we only want to retrieve the top-level categories of these article categories.
Example two: Retrieve the second-level categories under the product model.
If you want to display all second-level subcategories under the current category on a product category page, and these categories all belong to 'Product Model', you can operate in this way:
{# 假设当前页面已经识别出所属的产品分类,我们想显示其下的子分类 #}
{# 首先,获取当前页面的分类ID,通常页面上下文会自动提供,这里假设为 currentCategoryId #}
{# 或者,如果您在产品列表页,可以直接使用 item.Id 来获取当前遍历的分类ID #}
{% categoryList subCategories with moduleId="2" parentId=currentCategoryId %}
<div class="product-sub-categories">
<h3>当前分类的子分类:</h3>
<ul>
{% for subItem in subCategories %}
<li>
<a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
<span> (包含{{ subItem.ArchiveCount }}款产品)</span>
</li>
{% else %}
<li>当前产品分类下暂无子分类。</li>
{% endfor %}
</ul>
</div>
{% endcategoryList %}
Here:
moduleId="2"Make sure we get the category that is exclusively for the product model.parentId=currentCategoryIdThen dynamically get the category ID of the current page and display its direct subcategories.currentCategoryIdReplace with the actual current category ID variable, it can be used directly on the list pageitem.Id.
More than just a list: rich display of category information
Once you pass throughmoduleIdSuccessfully filtered the required category list, you can use it in the loopitemTo enrich the display of category information, use various properties of the object, for example:
{{ item.Title }}: Display the category name.{{ item.Link }}:Generate the link to the category detail page.{{ item.Description }}:Display the category introduction.{{ item.Logo }}or{{ item.Thumb }}:Show the cover image or thumbnail of the category, suitable for categories that require images.{{ item.ArchiveCount }}: Display the number of documents (articles, products, etc.) under the category.{{ item.HasChildren }}: Determine if the current category has subcategories for conditional rendering.
The flexible application of these properties can make your category list not only powerful in function, but also beautiful in interface and rich in information, greatly enhancing the user experience.
Summary
Of Security CMScategoryListLabel combinationmoduleIdParameters provide website operators with extremely high flexibility, enabling precise control over the classification and display of content based on different business needs and content models.This not only makes your website structure clearer, but also greatly simplifies the complexity of template development and improves the efficiency of content management.Master this skill, and you will be able to build a powerful and well-organized website content architecture more freely, thus better serving your users and business goals.
Frequently Asked Questions (FAQ)
1. How do I determine the corresponding 'article model' or 'product model' ofmoduleIdhow much it is?
In most cases, the 'article model' built-in in the AnQi CMS ismoduleIdIs1while the "product model" ismoduleIdIs2. If you want to confirm or view the ID of the custom model, log in to the Anqi CMS backend and go to the 'Content Management' -> 'Content Model' page.Each content model will display its ID next to its name or in the URL of the detail page.
2.parentIdThe parameter is incategoryListWhat is the role? How should I set it to display all categories?
parentIdThe parameter is used to specify the retrieval of child categories under a specific parent category. WhenparentId="0"It will retrieve all top-level categories. If you want to retrieve all subcategories of a specific category, you canparentIdSet the ID of the parent category. If you want to display all categories under all models, regardless of the level, you can useall=trueparameters, for example{% categoryList categories with moduleId="1" all=true %}. But please note,all=trueIt will return all levels of categories, you may need to handle the level display logic in the template.
3. Can I be in acategoryListTags show categories from both the 'Article Model' and 'Product Model' at the same time?
NomoduleIdThe parameter is for filtering a single content model. OnecategoryListOnly one tag can be specified.moduleIdIf you indeed need to display categories from different content models in the same list, you need to use two (or more) separatecategoryListtags to specify differentmoduleIdThen, merge them manually or process them through JavaScript on the frontend.For example, you can first get the list of article categories, then get the list of product categories, and then render or combine them separately in the template.