How to display the name of a specified category

Calendar 👁️ 66

Flexible display of category names in AnQiCMS: A comprehensive template call guide

Content categories are the core of website structure, not only helping to organize and manage massive amounts of information, but also guiding users to browse the website and improving user experience.Whether it is an article list, product details, or side navigation, clearly displaying the category names can make the content of your website more organized.AnQiCMS with its concise and efficient template engine, provides various flexible ways to call and display the names of specified categories, allowing you to easily achieve customized content display according to different scene requirements.

Next, we will start from several common scenarios and discuss in detail how to display category names in the AnQiCMS template.

1. Display the category name of the current page.

When you are on a category list page (such as displaying all articles under the "News Information" category), it is often necessary to display the name of the current category at the top of the page. AnQiCMS providescategoryDetailLabel, makes this operation very intuitive.

If you want to display the current category title, just use it like this in the template:

<div>当前分类名称:{% categoryDetail with name="Title" %}</div>

Herename="Title"Tell the system the category title you need to retrieve. As it is not specifiedidortokenThe parameter, the system will automatically identify and retrieve the corresponding category information on the current page

Second, display the name of the specified category

Sometimes, you may need to display the name of a specific category on non-category pages (such as the homepage, article detail page), such as the link text of a fixed category in the website footer navigation.At this time, it can be accurately obtained through the category ID or URL alias.

To get the name by category ID, assuming your "About Us" category ID is 5, you can call it like this:

<div>指定分类名称(ID为5):{% categoryDetail with name="Title" id="5" %}</div>

If your category is configured with a URL alias (for example, a category named "Industry News", the URL alias isindustry-news), you can also use this alias to get its name:

<div>指定分类名称(别名为industry-news):{% categoryDetail with name="Title" token="industry-news" %}</div>

In this way, you can flexibly display the specific category name you want to show at any location on the website.

3. Display the category name belonging to the content (article, product) details page

When a user browses a specific article or product, they usually want to know which category it belongs to, in order to better understand the content context or navigate to related categories.AnQiCMS provides the ability to directly obtain the category information of the document (article or product) on the detail page.

In the document detail template, you can usearchiveDetailtags to get all the classification information of the current document, and then extract the classification title:

{% archiveDetail archiveCategory with name="Category" %}
<p>所属分类:<a href="{{ archiveCategory.Link }}">{{ archiveCategory.Title }}</a></p>

Here, we first assign the current document's category object toarchiveCategorythe variable, then througharchiveCategory.Titleit can easily display the category name, at the same timearchiveCategory.Linkand can also be used to build category links, convenient for users to click and jump.

Four, display the category name of each item in the content (article, product) list page

The homepage and category list page of the website usually display multiple articles or products, and each item will indicate its category below.In this loop display scenario, AnQiCMS can also make it easy for you to implement.

You can usearchiveListTag to get the article list, thenforCombining in the loopcategoryDetailTag, display the category name of each article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <div>
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>
            所属分类:
            {# 通过 item.CategoryId 获取分类ID,再用 categoryDetail 获取分类名称和链接 #}
            {% categoryDetail currentCategory with name="Title" id=item.CategoryId %}
            {% categoryDetail categoryLink with name="Link" id=item.CategoryId %}
            <a href="{{ categoryLink }}">{{ currentCategory }}</a>
        </p>
        <p>{{ item.Description }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In the above example,item.CategoryIdIt will provide the current article's category ID in the loop, and then we will use this ID tocategoryDetailget the corresponding category name and link in the tag.

V、Display the names of the multi-level category list

In complex navigation or sidebar scenarios, you may need to display multi-level category lists, including top-level categories and their subcategories.categoryListTags can also handle it.

For example, to display the names of all top-level categories and their first-level subcategories:

{% categoryList topCategories with moduleId="1" parentId="0" %} {# 获取文章模型的所有顶级分类 #}
    <ul>
        {% for topItem in topCategories %}
        <li>
            <a href="{{ topItem.Link }}">{{ topItem.Title }}</a>
            {% if topItem.HasChildren %} {# 判断当前分类是否有子分类 #}
                <ul>
                    {% categoryList subCategories with parentId=topItem.Id %} {# 获取当前顶级分类的子分类 #}
                        {% for subItem in subCategories %}
                        <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                        {% endfor %}
                    {% endcategoryList %}
                </ul>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

Here, we implement the display of multi-level category names elegantly through nesting:categoryListTags andtopItem.HasChildrenJudgment, elegantly implements the display of multi-level category names.

Summary

The AnQiCMS template system provides a powerful and flexible tag set, allowing you to easily display category names in various corners of the website.Whether it is to obtain detailed information of a single category, or dynamically display on the list page, or build a multi-level navigation structure, these tags can greatly simplify your template development work.Master these calling methods, and you will be able to design and optimize your website content layout more freely.


Frequently Asked Questions (FAQ)

Q1: Why is my category name not displayed?A1: If the category name is not displayed normally, first check your template code.categoryDetailorcategoryListThe label parameter is correct. EnsureidortokenThe value of the parameter matches the category ID or URL alias set in your backend. Next, checkname="Title"Is the spelling correct, and has the template file been uploaded and activated correctly.In some cases, it may be due to cache reasons, try to clear the system cache of AnQiCMS background, or refresh the browser cache.

Q2: Can I display other information about the category besides the category name? For example, the category description or image?A2: Of course you can.categoryDetailLabels not only support retrievalTitle, but can also retrieve other fields of the category, such asDescription(Category Description),Link(Category link),Logo(Category thumbnail large image),Thumb(Category thumbnail), even if you customize the fields for categories in your backend content model. You just need to changenamethe parameter to the corresponding field name. For example, to display the category description:{% categoryDetail with name="Description" %}.

Q3: How can I get all the subcategory names of a certain category but not include the category itself?A3: IncategoryListYou can filter through the tags.parentIdParameters to specify the retrieval of subcategories of a certain category. For example, if you want to get the subcategories of IDXall subcategory names of the category, the code would be:{% categoryList subCategories with parentId="X" %}in a loop.subCategorieswhen it will only include its subcategories and will not include the category with IDXthe category itself.

Related articles

How to set pagination for a document list and display page navigation?

In modern website operations, setting up pagination for content lists is almost indispensable.It can not only significantly improve the user's browsing experience, avoid the slow loading caused by the long content of a single page, but also help search engines better crawl and index the website content.For friends using AnQi CMS, implementing pagination for the document list and displaying page navigation is a very intuitive and powerful feature.Our AnQi CMS provides flexible template tags, allowing full control over content display.To set pagination for the document list and display page navigation in a friendly manner

2025-11-08

How to implement multi-condition (such as custom parameters) filtering function on the document list page?

In AnQi CMS, implementing multi-condition filtering functionality on the document list page, especially filtering based on custom parameters, is crucial for improving user experience and helping users quickly find the content they need.AnQi CMS with its flexible content model and powerful template tag system makes this feature highly efficient and intuitive.The entire process can be divided into several core steps: First, define your content model and custom filter fields in the background;Secondly, build the display interface for the filtering conditions in the template; finally, present the filtering results in the document list using list tags.--- ### One

2025-11-08

How to implement keyword search and display results on the document list page?

How to help visitors quickly and accurately find the information they need in the face of increasingly large amounts of website content is the key to improving user experience and website value.AnQiCMS (AnQiCMS) is deeply proficient in content management, providing us with a powerful and flexible keyword search mechanism that allows the document list page to easily implement keyword search and elegantly present the results.

2025-11-08

How to filter and display a document list based on specific recommendation attributes (such as 'Headline', 'Recommendation')?

The flexible application of content recommendation properties in AnQiCMS is a key link in enhancing the visibility and user experience of website content.Whether you want to highlight important news on the homepage or recommend selected content in specific sections, AnQiCMS provides a convenient and efficient solution.This article will delve into how to filter and display document lists based on specific attributes such as 'Headlines' and 'Recommendations', helping you better manage and present website content.### Part One: Understanding Recommendation Properties and Their Settings Recommendation properties are a very practical feature in the AnQiCMS content management system

2025-11-08

How to define a string array directly in the AnQi CMS template?

In AnQi CMS template development, we often encounter the need to display some list data on the page, such as navigation menus, category tags, or some fixed options.When this data is not suitable for dynamic retrieval from the backend database, or when we just need a simple and fixed set of strings, defining a string array directly in the template is very convenient and efficient.Our Anqi CMS provides a flexible way to handle such requirements based on the Django template engine syntax.

2025-11-08

Can 'list' filter be used to define an array that includes numeric types in the AnQi CMS template?

During the development of AnQi CMS templates, we often need to handle various data, among which arrays (or lists) are commonly used to organize data.When it comes to defining an array containing different data types directly in a template, many friends may be curious whether types like numbers can also be processed and included in the `list` filter?The answer is affirmative.The `list` filter in Anqi CMS template is designed to be very flexible, it fully supports including numeric data in the defined array.

2025-11-08

How to iterate over an array defined in an AnQi CMS template using the `list` filter?

In Anqi CMS template design, we often encounter situations where we need to define and iterate over some fixed or small array data.Although most dynamic content is retrieved through built-in tags such as `archiveList`, `categoryList`, etc., it is more convenient to directly process arrays in the template for some localization configurations, custom options, or static lists.AnQi CMS provides the `list` filter, combined with the `for` loop tag, it can easily meet this requirement.

2025-11-08

How would AnQi CMS handle if the string format is incorrect when using the `list` filter to define an array?

In website content operation, we often need to display various list data in templates, such as a set of keywords, product features, or navigation links.AnQiCMS provides a powerful template engine, where the `list` filter is a very practical feature, allowing us to directly convert string-formatted data into an iterable array object in the template, greatly enhancing the flexibility of the template.

2025-11-08