As an experienced CMS website operation personnel, I am well aware of the importance of content organization and user experience for the success of the website.Accurately display content categories, which can not only help users quickly find the information they are interested in, but also improve the overall SEO performance of the website.Today, we will delve into how to obtain the category list under specific content models (such as articles, products) in AnQiCMS, which is crucial for building clear website navigation, content aggregation modules, and special pages.
Understand the content model and classification system of AnQiCMS
AnQiCMS provides great flexibility in content management, one of its core features is the 'flexible content model'.This means you can customize content types according to your business needs, for example, in addition to the default article and product models, you can also create various models such as events and cases.This content model is the fundamental structure of your website content.
The content model is closely associated with the classification system.In AnQiCMS, each category clearly belongs to one and only one content model.This means that when you create a "newsThis design ensures the high organization of content, avoids confusion in the classification of different types of content, and makes content management and frontend display clearer and more efficient.
UtilizecategoryListTag to get the classification list
In AnQiCMS template design,categoryListtags are the core tools for obtaining category lists. It allows you to filter and sort categories based on various conditions, one of the most critical parameters beingmoduleIdIt allows you to accurately specify which content model category to retrieve.
To usecategoryListTag, you need to call it in the template file in the following basic format:
{% categoryList 变量名称 with moduleId="模型ID" %}
{# 循环输出分类数据 #}
{% for item in 变量名称 %}
<a href="{{ item.Link }}">{{ item.Title }}</a>
{% endfor %}
{% endcategoryList %}
here,变量名称Is the arbitrary name you specify for the category list obtained, for examplecategories.moduleIdThe parameter is used to specify the content model ID you want to obtain for the category list.
Determine the content model ID
In AnQiCMS, each content model has a unique numeric ID. Typically, the ID for the 'Article Model' is set to default as1,“Product Model” is set to default as2This may vary depending on your system configuration or the creation order of the custom model.
To accurately obtain the ID of your content model, you need to log in to the AnQiCMS admin interface.In the 'Content Management' section, find the 'Content Model' menu.After entering this page, you will see all the defined content models and their details, usually including their IDs.Record the corresponding ID of the "article" or "product" model you need to query, so that it can be used in template tags.1thenmoduleIdThe value of the parameter should be set to"1".
Actual application: Get the list of articles and product categories
MasteredmoduleIdAfter the usage, getting the classification list of a specific content model becomes very direct.
Get the classification list under the article model.
Assuming your "article" model ID is1And you want to display the top-level category (i.e., categories without a parent category), you can call it like this in the template:
<div class="article-categories">
<h3>文章分类</h3>
<ul>
{% categoryList articleCategories with moduleId="1" parentId="0" %}
{% for category in articleCategories %}
<li><a href="{{ category.Link }}">{{ category.Title }}</a></li>
{% endfor %}
{% endcategoryList %}
</ul>
</div>
This code will iterate through all top-level categories belonging to the article model and display their names and links in an unordered list.parentId="0"The function here is to limit only the top-level categories without any parent category displayed.
Get the category list under the product model
Similarly, if the ID of your "product" model is2You want to get all categories (including subcategories) under it? Then you can do it like this:
<div class="product-categories">
<h3>产品分类</h3>
<ul>
{% categoryList productCategories with moduleId="2" all=true %}
{% for category in productCategories %}
<li><a href="{{ category.Link }}">{{ category.Spacer | safe }}{{ category.Title }}</a></li>
{% endfor %}
{% endcategoryList %}
</ul>
</div>
Here, all=trueParameter indicationcategoryListTags get all categories under the specified model, not just top-level categories.{{ category.Spacer | safe }}Used to add indentation before categories with hierarchical relationships to visually distinguish parent-child categories.
Build hierarchical navigation by combining parent category parameters.
exceptmoduleId,categoryListTags also supportparentIdParameters, which is very useful for building multi-level category navigation. You can first get the top-level categories, and then call inside the loop of each top-level category againcategoryListto get its subcategories.
<nav class="main-navigation">
{% categoryList topLevelCategories with moduleId="1" parentId="0" %}
<ul>
{% for topCat in topLevelCategories %}
<li>
<a href="{{ topCat.Link }}">{{ topCat.Title }}</a>
{% if topCat.HasChildren %}
<ul class="sub-categories">
{% categoryList subCategories with parentId=topCat.Id %}
{% for subCat in subCategories %}
<li><a href="{{ subCat.Link }}">{{ subCat.Spacer | safe }}{{ subCat.Title }}</a></li>
{% endfor %}
{% endcategoryList %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
{% endcategoryList %}
</nav>
This code first retrieves all top-level categories under the article model. For each top-level category, if it contains subcategories (viatopCat.HasChildrenjudgment), it will call againcategoryListSet the ID of the current top-level category asparentIdTo retrieve and display its direct subcategories.
The thoughts and **practice of operation personnel
As a website operator, effectively utilizing these tags can greatly simplify the maintenance work of the website.For example, when you need to update the website navigation or display the classification of a product series on a specific page, there is no need to manually modify the HTML. Simply adjust the classification structure in the AnQiCMS backend, and the front-end page will automatically update.
We recommend that you plan the corresponding relationships between various content models and categories at the beginning of template development, and clearly name the categories in the background, so that you can quickly locate the requiredcategoryListtags when neededmoduleIdandparentIdAt the same time, by utilizing the modular design of AnQiCMS, the commonly used category list code is encapsulated in local templates, throughincludeLabel reuse can further improve development efficiency and code maintainability.
In summary, AnQiCMS providescategoryListtags and theirmoduleIdParameters are a powerful tool for website operators and template developers to efficiently manage and display specific content model categories.By using it flexibly, you can build a clear and user-friendly content display interface, thereby enhancing your website's performance in content marketing and user experience.
Frequently Asked Questions (FAQ)
1. How can I find the specific content model in the AnQiCMS backend?moduleId?
To find the ID of a specific content model, you can log in to the AnQiCMS backend and navigate to the 'Content Management' menu under the 'Content Model' page.On this page, the system lists all created or built-in content models, usually each model has a corresponding numeric ID.1The 'product model' is usually2If the ID cannot be seen directly, you can try clicking to edit a model, the URL may contain its ID, or it may be explicitly marked on the system interface.
2. Can I display categories from different content models in the samecategoryListtag? The design of the tag is for categorization of a single content model, so you cannot display categories from different content models in the same
categoryListtag in the samecategoryListSpecify multiple tags directlymoduleIdTo get a mixed classification. If you need to display classifications from different content models, you should call each content model separately.categoryListLabel, then combine or display their results independently in the front-end template.
3. How can I ensure that the category list I get only contains categories with content?
categoryListThe label itself does not provide parameters for directly filtering out empty categories. If you want to display only categories containing documents, you canforuseitem.ArchiveCountattributes to make judgments.ArchiveCountRepresents the number of documents associated with the category. For example:
{% categoryList categories with moduleId="1" parentId="0" %}
{% for category in categories %}
{% if category.ArchiveCount > 0 %}
<li><a href="{{ category.Link }}">{{ category.Title }} ({{ category.ArchiveCount }})</a></li>
{% endif %}
{% endfor %}
{% endcategoryList %}
This code ensures that the category will only be displayed if the number of documents in the category is greater than 0.