How to use the `categoryList` tag to display the list of articles/products categories on the homepage or sidebar?

Calendar 👁️ 72

When building and optimizing a website, a clear navigation structure is crucial for user experience and search engine optimization. AnQiCMS (AnQiCMS) provides us with powerful template tags, includingcategoryListTags are a core tool used to flexibly display article or product category lists on the website homepage or sidebar.With it, we can easily organize the website content neatly, allowing visitors to find the information they are interested in at a glance.

Why is the classification list so important?

Imagine when a visitor comes to a rich content website, if all the articles or products are piled up together without clear categories, they may feel at a loss and leave quickly.A well-structured classification list that can help users quickly locate information, improve browsing efficiency, and convey the organization logic of the website content to search engines, thereby improving the website's crawling efficiency and ranking.Whether it is displaying the latest information in the "News Center" or listing product categories in the "Product Display",categoryListCan be used for everything.

MastercategoryListBasic usage of tags

categoryListThe core function of tags is to retrieve the classification data set up on our backend. Its basic structure is usually a with{% categoryList ... %}and ends with{% endcategoryList %}The block ends. Inside this block, we can iterate through each category information obtained by the loop.

Let's take a look at some key parameters of this tag, which give us great flexibility:

  • moduleId: This is the key parameter of the specified content model. AnQi CMS supports various content models such as articles, products, etc. By settingmoduleIdWe can precisely tell the system which type of content classification we want to obtain. For example, if you want to display article categories, you would usually usemoduleId="1"; if it is product classification, it may bemoduleId="2"(The specific ID can be viewed in the "Content Management" -> "Content Model" in the background).
  • parentId: This parameter is used to control the hierarchical relationship of categories.
    • When we want to display only the top-level categories of the website, we can setparentId="0". This is very common on the homepage or main navigation.
    • If we want to get the child categories under a specific category, we can assign the ID of the parent category toparentId.
    • Further, if on a category list page, we want to display the 'brother' category of the current category, we can setparentId="parent".
  • allAs the name suggests, when set toall=trueAt that time, it will retrieve all categories under the specified model, regardless of the level. This may be used for special display requirements, such as generating a website map.
  • limit: This parameter allows us to control the number of categories displayed, such aslimit="10"It will only display the first 10 categories. For the list page, it also supports offset mode, such aslimit="2,10"Indicates starting from the second data item to fetch 10 items.
  • siteId: For users using the AnQi CMS multi-site management function,siteIdCan help us call the classification data under a specific site. Generally, we do not need to set it manually.

Traverse and display classification information

When we usecategoryListAfter the tag gets the classification data, it will usually combineforLoop through these data, and display the detailed information of each category. In the loop, we can access the attributes of each category throughitemvariables, such as:

  • {{item.Id}}: The unique identifier of the classification.
  • {{item.Title}}: The category name, this is the content displayed most frequently.
  • {{item.Link}}: The access link to the category page, convenient for users to click and jump.
  • {{item.Description}}: A brief description of the category.
  • {{item.Logo}}or{{item.Thumb}}: The thumbnail or Logo image link corresponding to the category.
  • {{item.HasChildren}}: A boolean value indicating whether the category has a subcategory, which is very useful when building multi-level navigation.
  • {{item.IsCurrent}}Indicates whether the current category is the category on the current page, which can be used to add a highlight style to the current category.
  • {{item.ArchiveCount}}The number of articles or products included in the category.

Practical Application: Display Category List on Homepage or Sidebar

Let's demonstrate how to apply it through several practical examplescategoryList.

1. Display Top Article Categories on Homepage or Sidebar

Suppose we want to display all top-level article categories in the sidebar on the homepage, so that users can quickly understand the main content direction of the website.

<div class="sidebar-block">
    <h3>文章分类</h3>
    <ul>
        {% categoryList categories with moduleId="1" parentId="0" %}
        {% for item in categories %}
        <li>
            <a href="{{ item.Link }}">{{ item.Title }} ({{ item.ArchiveCount }})</a>
        </li>
        {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

This code first goes throughmoduleId="1"Specify what we need to get the article category, then go throughparentId="0"Make sure to get only the top-level categories. In the loop, we displayed the name of each category and the number of articles it contains, and linked to the corresponding category page.

2. Display product categories and subcategories in the sidebar

For product display websites, we may need to provide more detailed product category navigation in the sidebar, including multi-level subcategories.

<div class="sidebar-block">
    <h3>产品分类</h3>
    <ul>
        {% categoryList productCategories with moduleId="2" parentId="0" %}
        {% for parentItem in productCategories %}
        <li>
            <a href="{{ parentItem.Link }}">{{ parentItem.Title }}</a>
            {% if parentItem.HasChildren %}
            <ul class="sub-categories">
                {% categoryList subCategories with parentId=parentItem.Id %}
                {% for subItem in subCategories %}
                <li>
                    <a href="{{ subItem.Link }}">{{ subItem.Title }}</a>
                </li>
                {% endfor %}
                {% endcategoryList %}
            </ul>
            {% endif %}
        </li>
        {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

In this example, we first retrieve the top-level product categories. When iterating through each top-level category, we checkparentItem.HasChildrenwhether it has child categories. If there are any, we make an nested call.categoryListAnd, toparentIdthe current top-level category toId(i.e.),parentItem.IdThus, you can obtain and display the sub-categories below. This nested structure can very intuitively display a multi-level classification system.

3. Combine the category list with the latest articles displayed.

Sometimes, we not only want to display the category itself, but also hope to show several of the latest articles under each category, which is very common in the home page content aggregation area.

<div class="homepage-section">
    {% categoryList categories with moduleId="1" parentId="0" limit="4" %}
    {% for catItem in categories %}
    <div class="category-block">
        <h3><a href="{{ catItem.Link }}">{{ catItem.Title }}</a></h3>
        <ul>
            {% archiveList articles with type="list" categoryId=catItem.Id limit="5" %}
            {% for artItem in articles %}
            <li>
                <a href="{{ artItem.Link }}">{{ artItem.Title }}</a>
                <span class="publish-date">{{ stampToDate(artItem.CreatedTime, "2006-01-02") }}</span>
            </li>
            {% empty %}
            <li>该分类下暂无文章。</li>
            {% endfor %}
            {% endarchiveList %}
        </ul>
    </div>
    {% endfor %}
    {% endcategoryList %}
</div>

Here we first obtained 4 top-level article categories. Inside the loop of each category, we again usedarchiveListtags to get the latest 5 articles under the category (throughcategoryId=catItem.Idandlimit="5")stampToDateThe filter helps us format the article's publication timestamp into a readable date.

Summary

categoryListTags are one of the foundations of Anqi CMS content operation, providing us with the powerful ability to build clear and efficient website navigation.By flexibly using its parameters and combining with other tags, we can achieve a variety of needs from simple classification displays to complex list aggregations.A meticulously designed category list, which not only enhances the user's browsing experience but also contributes to the website's SEO performance.


Frequently Asked Questions (FAQ)

1. Why isn't any content displayed in my category list?

First, check if the corresponding article or product category has been created in your background. Second, ensuremoduleIdThe parameter is set correctly, it tells the system which type of classification you want to retrieve (for example, articles are usuallymoduleId="1", products aremoduleId="2"). If you only want to display top-level categories, please confirmparentId="0"The parameter has been added correctly.

How to highlight the corresponding category in the sidebar for the current visited category page?

IncategoryListIn the loop, eachitemincludes oneIsCurrentProperty. You can use this property to add CSS classes, for example:<li><a href="{{ item.Link }}" class="{% if item.IsCurrent %}active{% endif %}">{{ item.Title }}</a></li>This link in the sidebar will be highlighted when the user visits a category page.

3. How do I display all subcategories under a category, regardless of the hierarchy level?

categoryListThe tag itself only supports direct access to one level of subcategories (by specifyingparentId)。If you need to display an infinite level of subcategories, you need to implement recursion or combine other logic in the template. The usual approach is to call the loop againcategoryList,to display the currentitem.IdasparentIdEnter and cooperateitem.HasChildrenMake a judgment. However, for most websites, two to three levels of classification is enough to provide clear navigation.

Related articles

How to automatically generate breadcrumb navigation using the `breadcrumb` tag and customize the display text on the homepage?

The user experience of a website largely depends on the convenience and clarity of navigation.When the visitor delves into the website content, a friendly 'Breadcrumb Navigation' can clearly indicate the current position and provide a quick path to return to the upper page.AnQiCMS is well-versed in this, providing a powerful and easy-to-use `breadcrumb` tag that allows website administrators to automatically generate paths without complex coding, and even to flexibly customize the display text on the homepage.

2025-11-08

How to use the `navList` tag to build a website navigation menu with secondary dropdown?

Build a clear and easy-to-use website navigation menu is an important link to improve user experience and website professionalism.In AnQi CMS, we can take advantage of the powerful `navList` tag to easily implement a navigation menu with secondary dropdown support, making the website content well-structured and convenient for visitors to browse.Next, we will explore how to use the `navList` tag, from backend configuration to frontend template rendering, step by step to create a complete second-level dropdown navigation.

2025-11-08

How to dynamically generate the page's Title, Keywords, and Description (TDK) to optimize SEO display?

In website operation, Title (title), Keywords (keywords) and Description (description) - abbreviated as TDK, are the core elements of search engine optimization (SEO).They directly affect the display of the website on the search engine results page (SERP), thereby determining the click-through rate and traffic of the website.For a content-rich website, manually setting TDK for each page is neither realistic nor efficient.

2025-11-08

How to conveniently display the company's contact phone number and address with the `contact` tag?

In website operation, clearly and conveniently displaying the company's contact information is a key link to enhance user trust and promote business communication.Whether it is to seek cooperation, provide customer support, or simply to let visitors understand the location of the company, contact information plays a vital role.AnQiCMS fully understands this and provides us with an extremely convenient way to manage and display this information through its powerful template tag system.

2025-11-08

How to retrieve and display the name, description, and thumbnail of a specific category?

In website operations, clearly and effectively displaying classification information is crucial for user experience and search engine optimization.A specific category name can help users quickly understand the content theme, a concise description can provide more background information, and a direct thumbnail can attract users to click.AnQiCMS (AnQiCMS) provides a powerful and flexible template tag feature, allowing you to easily obtain and present these key classification information, whether it is to build a navigation menu, design a category list page, or optimize search engine results.###

2025-11-08

How are the `pageList` and `pageDetail` tags used to display single-page content such as 'About Us'?

In website operation, content such as 'About Us', 'Contact Information', or 'Service Introduction' usually exists as independent pages, which we call 'single pages'.They do not update as frequently as blog posts, but they are essential core information for the website.AnQi CMS provides the `pageList` and `pageDetail` tags, allowing you to manage and display these single-page contents flexibly and efficiently.

2025-11-08

How does the `archiveList` tag display the article list based on categories, recommended attributes, or custom sorting?

In the Anqi CMS template system, the `archiveList` tag is the core tool for building dynamic content lists.Whether it is a blog article list, product display page, or news information module, `archiveList` can help you accurately filter, sort, and present content according to your diverse needs, bringing vitality to your website content area.

2025-11-08

How to display article title, content, images, etc. on the document detail page using the `archiveDetail` tag?

Manage website content in Anqi CMS, the document detail page is undoubtedly the core position for users to obtain information.How to present each carefully written article, its title, content, images, and various additional information accurately and vividly to the user, this requires the clever use of our `archiveDetail` tag.This tag is specifically designed to display the details of a single document, and its flexible use will help you easily build a feature-rich and smooth experience detail page.

2025-11-08