Today, with the website content becoming increasingly rich, a clear and efficient navigation system is crucial for enhancing user experience and search engine optimization (SEO).Especially for websites with a large amount of content or multiple product categories, multi-level category navigation can help users quickly find the information they need while also showing the structure of the website to search engines.The Anqi CMS, with its flexible and powerful template engine, makes it simple and intuitive to build such a navigation.This article will delve into how to cleverly construct multi-level category navigation in the Anqi CMS template, and intelligently judge whether each category contains subcategories, making your website navigation more dynamic and intelligent.

Understanding the basic template of Anq CMS

The template system of Anqi CMS adopts a syntax similar to Django template engine, which is very friendly to developers and easy to get started.It allows us to dynamically display data through specific tags and variables.categoryListTags,ifandforThis logic control label. These labels together form the basis for us to implement complex navigation logic.

The core of building multi-level classification navigation:categoryListtags

categoryListTags are designed by AnQi CMS specifically to retrieve category lists, and they can filter out different models and levels of category data according to our needs.

  • Specify content model (moduleId):Your website may contain articles, products, and various content models. Categories under each model are independent.moduleIdParameters, you can accurately specify which content model category to retrieve. For example,moduleId="1"usually represents an article model,moduleId="2"May represent a product model.
  • Specify the parent category (parentId): This is the key to implementing multi-level navigation.
    • WhenparentId="0"When set to auto, you will get all top-level categories under the specified content model.
    • WhenparentIdWhen set to a specific category ID, it will retrieve all direct subcategories under that category.
    • If you want to get the sibling categories of a certain category (i.e., all subcategories that belong to the same parent category), you can do so when the parent category ID is known by using that parent category ID asparentIdReceivedcategoryListLabel.

Check if there is a child category in the classification:item.HasChildren

In a loopcategoryListWhen getting the classification data by tag, each category (usually named in the loop) has a very useful attribute:item) has a very useful attribute:HasChildren。This is a boolean value, if the current category has subcategories,HasChildrenthe value will betrue;otherwise,false.Using this property, we can flexibly control the display logic of navigation in the template, for example, only displaying the container of the second-level menu under items that have subcategories, thus avoiding unnecessary empty menus.

Actual operation: build multi-level category navigation

Now, let's take a specific example to demonstrate how to build a two-level or multi-level classification navigation in a template.

假设我们希望构建一个显示文章分类的导航菜单,其中包含一级分类及其下的二级分类:English

`twig {# Get all top-level categories under the article model (assuming moduleId is 1) #} {% categoryList categories with moduleId=“1” parentId=“0” %}