How to dynamically display the list of articles or products under a category in the navigation dropdown menu?

Calendar 👁️ 77

AnQi CMS: Create a dynamic dropdown navigation menu to make content accessible

In website operation, the navigation menu is the starting point for users to explore the content of the website, and it is also an important clue for search engines to understand the structure of the website.The traditional navigation menu is often a fixed link, and as the website content becomes richer, especially when there are many articles and product types, manually maintaining the content list in the dropdown menu becomes extremely cumbersome and prone to errors.The Anqi CMS provides us with a graceful and efficient solution with its powerful content management capabilities, allowing us to easily display lists of articles or products under categories in the navigation dropdown menu, greatly enhancing user experience and operational efficiency.

Understand the navigation and content association mechanism of AnQi CMS

One of the core advantages of AnQi CMS lies in its flexible content model and template tag system.To implement dynamic navigation, we first need to understand the relationship between navigation entries and actual content (categories, articles, products).In Anqi CMS, the navigation menu is not simply pointing to a fixed URL, it can directly link to the existing categories or single pages on the website.This means that when we link a main navigation item to a product category, the system can intelligently recognize this association and allow us to further obtain the subcategories or specific articles/products under this category at the template level.

Configure the navigation structure in the background

First, we need to complete the basic navigation structure settings in the Anqi CMS backend management interface.

  1. Create and manage categories and content modelsEnsure that you have created the required article or product categories and published the corresponding articles or products under these categories.Anqi CMS supports custom content models, you can define different article, product fields according to business requirements.
  2. Set the main navigation items: Go to the 'Website Navigation Settings' under 'Backend Settings'.Here, you can create or edit navigation categories (such as "default navigation" or "footer navigation"), and then add a main navigation link to your website.
  3. Associate navigation with categoriesIn adding or editing navigation links, select the link type as "Category Page Link".At this time, the system will provide a dropdown list for you to select the specific article category or product category you want to associate with.This step is the key to implementing a dynamic dropdown menu, which establishes a direct link between the navigation items and the background content categories.
  4. Create secondary navigation (optional)If your dropdown menu needs a deeper structure, such as having subcategories under main categories, and the subcategories display article lists, you can continue to create a second-level navigation for the "Category Page Link" in the background and associate it with specific subcategories.

After completing the background configuration, our navigation structure has already been set up, and the next step is to dynamically render content through the tags provided by Anqicms in the template file.

Display content dynamically through template tags

The Anqi CMS template engine is similar to Django syntax, using concise tags ({% ... %}) and variables ({{ ... }}To retrieve and display data. To implement a dynamic dropdown menu, we will mainly usenavList/categoryListandarchiveListthese tags.

Assuming our goal is to display all product categories under the main navigation item "Products" in the dropdown menu, and be able to list several of the latest products directly under each product category.

First, in your template file (usuallybase.htmlorheader.htmldefine the navigation part here), we can organize the code in this way:

{# 假设我们正在处理一个名为 'navList' 的导航列表,它通过 navList 标签获取 #}
<ul class="main-nav">
    {% navList navList with typeId=1 %} {# typeId=1 通常指默认导航类别 #}
    {%- for item in navList %}
    <li class="nav-item {% if item.IsCurrent %}active{% endif %}">
        <a href="{{ item.Link }}">{{item.Title}}</a>
        {%- if item.NavList %} {# 检查当前导航项是否有子导航(二级菜单) #}
        <ul class="nav-dropdown">
            {%- for inner in item.NavList %} {# 遍历二级导航项 #}
            <li>
                <a href="{{ inner.Link }}">{{inner.Title}}</a>
                {% if inner.PageId > 0 %} {# 如果二级导航项关联了一个分类ID #}
                    {# 获取该分类下的产品列表。moduleId='2' 假设为产品模型ID #}
                    {% archiveList products with type="list" categoryId=inner.PageId moduleId="2" limit="5" %}
                    {% if products %}
                    <ul class="category-product-list">
                        {% for product in products %}
                        <li><a href="{{product.Link}}" title="{{product.Title}}">{{product.Title}}</a></li>
                        {% endfor %}
                    </ul>
                    {% else %}
                    <p class="no-content">暂无相关产品</p>
                    {% endif %}
                    {% endarchiveList %}
                {% endif %}
            </li>
            {% endfor %}
        </ul>
        {% endif %}
    </li>
    {% endfor %}
    {% endnavList %}
</ul>

The logic of this code is as follows:

  1. {% navList navList with typeId=1 %}This line of code will retrieve all the main navigation items set in the background.typeId=1It usually corresponds to the default navigation.
  2. {%- for item in navList %}We iterate over each main navigation item.item.TitleIs the navigation name,item.Linkis the link address.
  3. {%- if item.NavList %}This is the key.item.NavListThe attribute will include all secondary navigation items under the main navigation item (if set in the backend). If there is one, a dropdown menu container will be rendered (for examplenav-dropdown)
  4. {%- for inner in item.NavList %}:We continue to traverse these second-level navigation items.inner.Titleandinner.LinkIs the name and link of the second-level navigation.
  5. {% if inner.PageId > 0 %}:If this second-level navigation item is explicitly linked to a category in the background(inner.PageIdThat is the category ID, then we can use this ID to retrieve the content.
  6. {% archiveList products with type="list" categoryId=inner.PageId moduleId="2" limit="5" %}: This is the core of dynamically obtaining the list of articles or products.
    • productsWe define a variable name for the list of content obtained.
    • type="list"It indicates getting a regular list instead of a paginated list.
    • categoryId=inner.PageIdIt specifies which category of content to get.
    • moduleId="2": Very important, here it clearly tells the system that we want to get the content under the "product" model (usually the "article" model ID is 1, the "product" model ID is 2, the specific ID can be confirmed in the "content model" on the back end). If you want to display articles, change it to the corresponding one.moduleId.
    • limit="5": Limit each category to display only 5 of the latest posts to avoid the dropdown menu being too long and affecting user experience.
  7. {% for product in products %}Finally, we traverse and display these obtained product titles and links. If there is no content,{% else %}the part will display "No related products available."

Further expansion: display articles/products under subcategories

If your requirement is to display a subcategory of a main category first in the dropdown menu, and then click on the subcategory to enter its article/product list, then you can adjustarchiveListthe calling method, or atitem.NavListpass throughinner.PageIdcall againcategoryListto get a deeper level of classification, and then call under these classificationsarchiveList.

For example, under a main navigation "solutions", we want to list the subcategories "industry solutions" and "technology solutions", and also display the titles of the下属 articles next to each subcategory:

`twig

Related articles

How to judge the current link status in the navigation list tags and add a highlight style?

In website operation, clear navigation is a key factor in improving user experience.When users can intuitively see their location on the current page, it not only reduces the sense of being lost but also improves the professionalism and usability of the website.Adding a highlight style to the current link in the navigation bar is a very effective way to achieve this goal.In Anqi CMS, highlighting the current link of the navigation item is actually simpler and more direct than you imagine.

2025-11-07

How to create and manage a website navigation menu and implement the display of secondary navigation?

The website's navigation menu, like the skeleton and landmark of the website, guides visitors to quickly find the information they need, and is the core of user experience and website information architecture.A clear and intuitive navigation system not only enhances user satisfaction, but is also crucial for search engine optimization (SEO).AnQiCMS was designed with this in mind from the outset, providing a flexible and easy-to-operate navigation menu management function, allowing website operators to easily build and maintain the navigation system of the website.

2025-11-07

How to control whether the website name suffix is displayed in the homepage title TDK tag?

If you are using AnQiCMS to build or manage your website, you may encounter a common and important issue: how to finely control whether the website name is displayed as a suffix in the `<title>` tag on the home page.This not only concerns the display effect of the website in search engine results, but also directly affects users' perception of the brand.The AnQi CMS, as a content management system that focuses on SEO optimization and flexibility, provides simple and powerful control capabilities in this aspect.First, let's clarify what is meant by the "website name suffix" mentioned here

2025-11-07

How to dynamically display Title, Keywords, and Description on the homepage to optimize SEO?

It is the first step in building a successful website to make it search engine friendly.And Title (title), Keywords (keywords), and Description (description) - usually abbreviated as TDK - are the core elements of a website's dialogue with search engines.They not only tell search engines about the content of your page, but also directly affect whether users click on your website in the search results.AnQi CMS knows this, and therefore built-in powerful TDK management features, allowing you to easily achieve the dynamic display and optimization of TDK.

2025-11-07

How to manage and display all documents in AnqiCMS and support filtering by title, model, and category?

In the era of explosive digital content, efficiently managing and displaying website documents is a core challenge for every operator.When you have a vast amount of articles, product descriptions, service instructions, or any form of site content, how to ensure that these contents are organized, easy to find, and can be filtered according to different needs, is directly related to user experience and operational efficiency.AnqiCMS, as a system specially designed for enterprise content management, provides a powerful and flexible solution in this aspect.

2025-11-07

How to display thumbnails, descriptions, and custom fields on the document list page?

In website operation, the content list page is a key link for users to obtain information and decide whether to delve deeper into browsing.A richly informative, well-structured list page that can greatly enhance user experience and content appeal.It is particularly important to display thumbnails, descriptions, and custom fields reasonably in the document list page of Anqi CMS to achieve this goal.Through the powerful and flexible template tag system provided by Anqi CMS, you can easily meet this requirement without complex programming knowledge, just need to make simple modifications and configurations to the template file.

2025-11-07

How to accurately retrieve and display the content of a specified ID or URL alias on the document detail page?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides users with a rich set of tools to build and manage websites.During website operations, we often need to precisely display the detailed content of a document on a specific page, whether it is the document being viewed on the current page or specified by a particular identifier (such as ID or URL alias).The template tag system of Anqi CMS, especially the `archiveDetail` tag, is the key to meeting this requirement.

2025-11-07

How to use the `archiveDetail` tag to display images, view counts, and publishing time on the document detail page?

In website operation, the document detail page is the core area that attracts users and conveys information.A rich content, intuitive detail page, which can not only significantly improve user experience, but also has a positive push on search engine optimization (SEO).AnQiCMS provides a powerful `archiveDetail` tag that allows us to easily obtain and display various document information, such as images, view counts, and publication time.Next, we will explore how to skillfully use the `archiveDetail` tag in the document detail page

2025-11-07