Flexibly obtain and display information of specified categories and their subcategory lists

The clear classification of website content is not only convenient for users to browse, but also greatly benefits search engine optimization (SEO).AnQiCMS powerful template system provides us with intuitive and easy-to-use tags, which can accurately obtain and display the required classification data.categoryDetailandcategoryList.

Core Tools:categoryDetailWithcategoryList

categoryDetailTags, as the name implies, are used to obtainsingleThe detailed information of a specified category. It helps us extract various attributes such as titles, descriptions, links, etc. of a specific category.categoryListTags focus on obtaininga group of

The detailed information of a specified category

Firstly, we may need to obtain the basic information of a specific category, such as its name, description, and link.categoryDetailLabels are our ideal choice. It allows us to precisely specify the target category by the unique ID (id), or URL alias (token) for classification.

For example, if you have an ID of5The classification, and if you want to display its title, description, and link on the page, you can write the template code like this:

{# 假设要获取ID为5的分类的详细信息 #}
{% categoryDetail myCategory with id="5" %}
<section class="category-detail-section">
    <h1>分类名称:{{ myCategory.Title }}</h1>
    <p>分类描述:{{ myCategory.Description }}</p>
    <a href="{{ myCategory.Link }}">前往{{ myCategory.Title }}分类页面</a>
</section>

In this code, we will use the ID of5The classification information is obtained and assigned.myCategoryThen, we can access and display the name, description, and link of the classification through themyCategory.Title/myCategory.DescriptionandmyCategory.Linkproperties with ease.categoryDetailLabels also provide other rich fields, such asContent(Rich text content of categories),Logo(Category Large Image),Thumb(Category thumbnails) and others, you can call them flexibly as needed.

Display the list of subcategories of the specified category

Obtained the detailed information of the specified category, the next step is usually to display all the subcategories under the category. At this point,categoryListthe tag shines brightly.categoryListPassparentIdParameters are used to specify which parent category's subcategories we want to retrieve.

Continuing with the above example, if we want to list the subcategories with ID of5category (i.e., themyCategoryThe direct subcategories of ) can be operated in this way:

<section class="sub-categories-list">
    <h2>{{ myCategory.Title }}的子分类</h2>
    <ul>
        {% categoryList subCategories with parentId=myCategory.Id %}
        {% for subItem in subCategories %}
        <li>
            <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
        </li>
        {% endfor %}
        {% endcategoryList %}
    </ul>
</section>

Here,categoryListThe label `