How to display the top-level category list of a specific content model on the homepage or sidebar?

Calendar 👁️ 60

Manage website content in Anqi CMS, sometimes we need to clearly display the top-level category list of a specific content model on the homepage or in the sidebar and other key positions.This not only effectively guides visitors to browse the website content, improve user experience, but also is an important aspect of website content architecture optimization and search engine friendliness.By using AnQiCMS's flexible template system and powerful tag functions, this requirement is met directly and efficiently.

Understanding the Core: Content Model and Classification

In AnQi CMS, the core of content management lies in the 'content model'.You can create different content models based on your business needs, such as 'article model' for publishing blogs, news, and 'product model' for displaying product information.Each content model can have an independent classification system. To display the top-level classification list of a specific content model, it is first necessary to clarify which content model's classification we want to display.

The concept of "category" is the cornerstone of organizing content. Top-level categories refer to categories without parent categories, usually serving as the highest level of the content system.

Key tools:categoryListTemplate Tag

The Anqi CMS template system uses a syntax similar to Django, obtaining and displaying data through various template tags. To get the category list, we need to usecategoryListThe tag is specifically designed to retrieve category data and provides rich parameters to finely control the output results.

The two most critical parameters are:

  • moduleId: Used to specify which content model category you want to retrieve. Different content models have unique IDs in the background.
  • parentId="0"This is a very important parameter, it tells the system that we only need to retrieve those without a parent category, that is, the 'top-level category'.

Step Details: Display Top Categories on the Homepage or Sidebar

Step 1: Determine the Content Model ID

First, we need to know the unique ID of the target content model. This is usually checked in the Anqie CMS backend management interface.

  1. Log in to the AnQi CMS background.
  2. Navigate to 'Content Management' -> 'Content Model'.
  3. Here, you will see the list of all created content models, such as "article model", "product model", and so on.Each model will have a corresponding ID (usually a number).For example, by default, the 'Article Model' ID may be 1, and the 'Product Model' ID may be 2.Remember the model ID you want to display for the category.

Step 2: Choose a suitable display location

You can choose to display in the main content area of the homepage(index.html) or the website's universal sidebar(partial/aside.htmlOr a file with a similar name, depending on the structure of your template), or display the category list at the header, footer, or other locations. If the list is general and needs to be reused across multiple pages, it is recommended to place it in a separate template fragment file (such aspartial/top_categories.htmlThen proceed{% include "partial/top_categories.html" %}The tag is used to include it in the page that needs to be displayed. This modular approach makes your template code cleaner and easier to maintain.

Step 3: Write the template code

Now, let's write the actual template code. Below is a general code example demonstrating how to obtain the top-level category of the 'article model' and display it as a linked list.

Assuming we want to display the "article model" (assuming an ID of1) top-level category list:

<div class="sidebar-block">
    <h3>文章分类</h3>
    <ul>
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li class="category-item">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {# 如果需要,可以判断当前分类是否有子分类,并显示一个指示符 #}
                    {% if item.HasChildren %}
                        <span class="has-children-indicator">›</span>
                    {% endif %}
                </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

Code analysis:

  • {% categoryList categories with moduleId="1" parentId="0" %}: This is the core tag, it will come from the ID of1In the content model, retrieve all top-level categories and store the results in a variable namedcategories.
  • {% for item in categories %}:TraversecategoriesEach category data in the variable.
  • item.Link: Output the URL link of the current category.
  • item.TitleOutput the name of the current category.
  • item.HasChildrenThis is a boolean value, if there are subcategories under the current category, it will returntrueYou can use this feature to add different styles or icons to items with subcategories.
  • {% endcategoryList %}:categoryListThe end tag that wraps the looping content of the list.

If you want to display several of the latest articles under each top-level category below, you can extend the code like this (assuming you want to display 3 articles):

<div class="sidebar-block">
    <h3>文章分类</h3>
    <ul>
        {% categoryList categories with moduleId="1" parentId="0" %}
            {% for item in categories %}
                <li class="top-category-item">
                    <a href="{{ item.Link }}">{{ item.Title }}</a>
                    {% if item.HasChildren %}
                        <span class="has-children-indicator">›</span>
                    {% endif %}
                    {# 显示该顶级分类下的最新文章 #}
                    <ul class="sub-articles">
                        {% archiveList articles with categoryId=item.Id type="list" limit="3" %}
                            {% for article in articles %}
                                <li><a href="{{ article.Link }}">{{ article.Title }}</a></li>
                            {% empty %}
                                <li>暂无文章</li>
                            {% endfor %}
                        {% endarchiveList %}
                    </ul>
                </li>
            {% endfor %}
        {% endcategoryList %}
    </ul>
</div>

Explanation of the extended code:

  • {% archiveList articles with categoryId=item.Id type="list" limit="3" %}: This is nested.archiveListLabel, it will be under the current category (determined byitem.Id) to get the latest 3 articles and store them inarticlesthe variable.
  • type="list": This indicates getting a regular list instead of a paginated list.
  • limit="3": Limit to displaying only 3 articles.
  • {% empty %} ... {% endarchiveList %}If:archiveListNo articles were retrieved, "No articles available" will be displayed.

By organizing the code in this way, you can easily display the top-level categories of a specific content model and its associated content at any location on the website, greatly enriching the form of content display on the website.

Practical suggestions

  • Style ControlThe above code only shows the structure, you need to combine CSS styles to beautify the display of the category list to match your website design.
  • Cache updateAfter modifying the template code, if the front-end page does not take effect immediately, please try to clear the system cache of Anqi CMS to ensure that the latest code is loaded.
  • Modular referenceIf your website has multiple sidebars or different pages that need to display similar category lists, you can consider saving the above code as a separate template file (for examplepartial/top_categories.htmlThen proceed{% include "partial/top_categories.html" %}In the required place, it improves code reuse and maintenance efficiency.

The Anqi CMS template tag system provides great flexibility, allowing you to organize and display website content according to your specific needs.Master these basic methods and you will be able to better utilize the potential of AnQiCMS.


Frequently Asked Questions (FAQ)

Q1: How to further display the subcategory list under the top category?A1: To display subcategories under the top-level category, you can in the{% for item in categories %}loop, use `category` again

Related articles

How can I build and display a multi-level nested category navigation menu?

Building and displaying multi-level nested category navigation menus in Anqi CMS is an important aspect of website content organization and user experience optimization.A well-designed, hierarchical navigation not only helps visitors quickly find the information they need, but also provides clear website structure signals to search engines, thereby improving SEO effectiveness. AnQi CMS is an enterprise-level content management system developed in Go language, with flexible content models and powerful template tags, making it very intuitive and efficient to implement multi-level classification navigation.Next, we will explore how to step by step build such navigation

2025-11-08

How to display the 'Previous' and 'Next' article links and titles on the article detail page?

It is crucial to enhance user experience and content discoverability in website operation.When a reader is immersed in an article, providing navigation links to the "previous article" and "next article" can not only guide them to continue browsing the website's content but also effectively reduce the bounce rate and optimize the on-site SEO structure.For those of us using the AnQiCMS website to manage, implementing this feature is both simple and efficient.AnQiCMS provides a straightforward and powerful template tag system, making it easy to integrate 'Previous Article' and 'Next Article' links on article detail pages

2025-11-08

How to call and display the custom fields of an article on the article detail page?

In Anqi CMS, the content display of the article detail page is highly flexible, especially for fields customized through the content model.These custom fields can help us manage and display different types of information more precisely, such as product prices, authors' origins, and property layouts, etc.How can you accurately call and display the content of these custom fields on the article detail page?This article will introduce this process in detail. ### 1. Custom field creation and filling: laying the content foundation In AnQi CMS

2025-11-08

How to display a list of related articles based on the keywords or custom associations of an article?

In website content operation, providing users with a list of relevant articles can not only effectively extend the visitor's stay time on the website and reduce the bounce rate, but also through guiding users to discover more interesting content, optimize the internal traffic path, and have a positive impact on SEO performance.AnQiCMS (AnQiCMS) understands this need and provides flexible and powerful functions, allowing us to easily display lists of related articles based on keywords or custom associations.###

2025-11-08

How to create and display single-page content such as “About Us” and “Contact Us”?

In website operation, single-page content like "About Us", "Contact Us", and the like are windows to showcase the corporate image, core values, or provide key information to visitors.They usually carry stable, independent and infrequent updated content, which is crucial for building trust and providing service access.AnQiCMS provides very convenient and powerful functions for managing and displaying these single-page applications, allowing you to easily create and customize them.### Part 1: Create Your Single Page Content Creating a single page in AnQiCMS is an intuitive and efficient process

2025-11-08

How to display all used Tag tags on the website or only display Tag tags related to the current article in AnQiCMS?

When building website content, tags play an important role. They can not only help you better organize information and improve the discoverability of content, but also optimize the user experience, making it easier for visitors to find the content they are interested in.For a content management system, flexibly displaying and managing these tags is one of its core values.AnQiCMS provides powerful functions in this aspect, allowing you to easily control the display of tags on your website according to your actual needs.### Add and manage Tag tags On AnQiCMS backend

2025-11-08

How to display a list of all related articles under a specific Tag tag?

In Anqi CMS, tags (Tag) are an important tool for organizing content, enhancing user experience, and optimizing search engine performance.It can link articles from different categories but with related themes, making it easier for visitors to find content they are interested in.When you need to display a list of all associated articles under a specific Tag label, AnQi CMS provides a concise and powerful template tag to help you achieve this goal.### Understanding the Core: `tagDataList` tag To display the article list under a specific Tag, we mainly use the built-in functions of Anqi CMS.

2025-11-08

How can I optimize the URL structure of the AnQiCMS website to improve search engine friendliness?

During the operation of a website, a good URL structure, like a clear map, can not only help users intuitively understand the content of the page, but also guide search engines to more efficiently crawl and understand your website.AnQiCMS was designed with SEO needs in mind from the beginning, built-in powerful URL management functions, allowing us to easily create a search engine-friendly website structure.Understanding why search engine friendly URLs are important Before delving into the specific features of AnQiCMS, let's talk briefly about it

2025-11-08