As an experienced website operation expert, I know that it is very important to flexibly display category lists when building the content structure of a website.AnQiCMS (AnQiCMS) provides us with great convenience with its powerful template tag system.categoryListLabel, elegantly build a flat list of all categories so that all categories are clearly displayed on your website without levels.

1. Why do we need a flat category list?

In content management, we often need a tree-like, hierarchical classification structure to organize content, which helps users gradually delve into specific topics. But sometimes, we also need a more direct, more comprehensive way to display all categories, such as:

  • Sitemap pageUsers want to see the scope of all the content on the website at a glance.
  • Footer navigation at the bottom of the pageProvide an overview of the website content, allowing users to quickly jump to any category.
  • Recommended content area: Guide users to discover more interests in the form of tag clouds or popular categories.
  • Data statistics or management viewIn some customized management interfaces, it may be necessary to view all categories without concerning their hierarchical relationships.

In this scenario, AnQiCMS'categoryListA label combined with a clever parameter can perfectly meet our needs.

Part two: UnveilingcategoryListThe core capability of the label

Of Security CMScategoryListThe tag is a universal tool used to obtain the classification list of content models such as articles, products, etc.It is powerful, can get child categories under a specific parent, and can also filter categories under a specific model.all=true.

Generally speaking, when we usecategoryListand specifyparentId="0"At this time, we get the top-level category. However, if your category structure is hierarchical, merely obtaining the top-level category is not sufficient to display all. At this point,all=trueThe parameter comes into play, it indicates that the system should ignore the hierarchical relationship and extract all matching categories at once.

At the same time, we also need to pay attention tomoduleIdThe parameter helps us accurately specify whether to retrieve all categories under the "Article Model" or all categories under the "Product Model", or categories under other custom models.This ensures that the list we obtain is relevant and meaningful.

Step 3: Get hands-on: Build a flattened classification list

Now, let's see how to use a real code example tocategoryListbuild a flattened list of all classifications.

Assuming we want to display all article category links at the bottom of the website, not hierarchical, in list form.

<div class="footer-categories">
    <h3>探索更多分类</h3>
    <ul>
        {#
            使用 categoryList 标签获取所有分类。
            - all=true: 告诉系统获取所有分类,忽略层级。
            - moduleId="1": 指定我们只获取“文章模型”下的分类 (通常文章模型ID为1,请根据您的实际后台配置调整)。
            - categories: 我们将获取到的分类列表命名为 categories,方便后续在for循环中使用。
        #}
        {% categoryList categories with all=true moduleId="1" %}
            {# 遍历获取到的每一个分类项 #}
            {% for item in categories %}
            <li>
                <a href="{{ item.Link }}" title="{{ item.Title }}">
                    {{ item.Title }}
                </a>
            </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

In this code block:

  1. We first defined adivA container to hold our flattened classification list.
  2. {% categoryList categories with all=true moduleId="1" %}Is the key point.
    • all=true: This is the core to achieve flattened display. It will.categoryListReturn all classifications, regardless of their level.
    • moduleId="1"Assuming your article model ID is 1. If you want to get the product categories, you may need to change it tomoduleId="2"(Please confirm the specific ID according to your Anqi CMS backend content model settings).This parameter ensures that we only obtain categories under specific content models, avoiding the mixing of unrelated categories (such as product categories mixed into article category lists).
  3. {% for item in categories %}We use a standardforto loop throughcategoryListlabel to return each category item.
  4. Inside the loop,{{ item.Link }}The link address of the category will be output,{{ item.Title }}The category name will be output. We have deliberately avoided usingitem.Spaceroritem.HasChildrensuch fields related to hierarchical structure to keep the list completely flat.

Through such settings, no matter how complex your classification structure is (first-level classification, second-level classification, third-level classification), the final display on the front end will be a simple classification list without hierarchical feeling.

Step 4: Advanced Thinking and Adjustment

  • Limit the Display Quantity: If your category number is large, you may not want to display all at once. You can addlimitparameters to control the number of categories displayed, for examplelimit="20"Display only the first 20 categories.
  • Specify multiple content models: If you need to display categories from different content models while still keeping them flat,categoryListCurrently does not support specifying multiple at the same timemoduleIdYou may need to call them separatelycategoryListTags, then merge the results, or manage the category sets that need to be displayed more finely through the backend.
  • Add simple styleThe above code only shows the structure, you can use CSS to.footer-categoriesbelowulandliAdd style to make it beautiful and generous, for example, adjust font size, spacing, or achieve horizontal alignment, etc.

Summary

Of Security CMScategoryListLabel collaborationall=trueParameters provide website operators with an extremely convenient way to build a flat list of all categories.This flexibility allows us to easily adjust the content display strategy of the website according to different operational needs, whether it is to improve SEO effects or to enhance the user navigation experience, it provides strong support.Master this skill, and you will be more proficient in website content operation.


Frequently Asked Questions (FAQ)

Q1: How can I only display the top-level categories instead of all categories?A1: If you want to display only the top-level categories, not all hierarchical categories, you need toall=trueremove the parameter and make sure that theparentId="0". For example:{% categoryList categories with moduleId="1" parentId="0" %}This way, the system will only return the top-level categories without any parent category.

Q2: Can I only get all categories under a specific content model (such as "Product Model")?A2: Of course you can. You just need to adjustmoduleIdthe parameters. For example, if your product model ID is2, you can use it like this:{% categoryList categories with all=true moduleId="2" %}This ensures that the list only contains product-related categories.

Q3: How to sort all flattened displayed categories, such as by name in alphabetical order?A3:categoryListThe tag itself in obtaining all categories (all=true) was not provided directly in the documentorderParameters to control the sorting method of the returned results.The display order of categories usually follows the category sorting you set in the AnQiCMS backend.If you need more complex sorting (such as by name initials), you may need to consider sorting the list you get on the front end through JavaScript, or customizing the query logic of the category list on the back end (this usually requires development support outside of the template engine).