How to retrieve and display the name, description, Banner image, and thumbnail of a specific category using the `categoryDetail` tag?

Calendar 👁️ 84

In AnQiCMS template creation, we often need to retrieve and display detailed information of specific categories, such as the name, description, and even preset Banner images and thumbnails.categoryDetailThe label was born for this, it can help us easily extract these data from the database and display them flexibly on the front end of the website.

UnderstandingcategoryDetailThe core role of tags

categoryDetailThe main function of the tag is to obtain detailed data of a single category.No matter if you want to display additional information for a category item on the category list page or retrieve all the data for the current category on the category details page, this tag is very useful.

Its basic usage form is usually like this:{% categoryDetail 变量名 with name="字段名" id="分类ID" %}

Among them:

  • 变量名This is an optional parameter. If you specify a variable name, for examplemyCategoryThen the classification data obtained by this label will be stored in this variable, and you can use it in the subsequent template code{{myCategory.Title}}This way to access the data. If no variable name is specified, the label will be output directlynameThe parameter specifies the value of the field.
  • nameThis is the most critical parameter, used to specify which field information you want to get from the category, such asTitle(Category name),Description(Category description) et al.
  • idThe same is optional. When you use a category page (such as a category detail page or category list page) andcategoryDetailif a tag is not specifiedidIt will automatically identify the category ID of the current page to retrieve data. But if you want to get the information of a specific ID category, you need to specify it explicitlyid="分类ID".
  • token: withidSimilarly, you can also specify the category to retrieve by using the category URL alias, for exampletoken="about-us".
  • siteIdIf you manage multiple sites and have created them through the multisite management feature in the backend, you can usesiteIdparameter to specify which site to get the category data from.

Get and display the category name and description

The category name and description are the most basic information of the category, and also an important part of search engine optimization (SEO) and user experience.

You can use to get the category namename="Title":

{# 假设我们想获取ID为1的分类名称 #}
<div>分类名称:{% categoryDetail with name="Title" id="1" %}</div>

{# 或者在分类详情页获取当前分类名称 #}
<div>当前分类名称:{% categoryDetail with name="Title" %}</div>

{# 将分类名称存储到变量中 #}
{% categoryDetail currentCategoryTitle with name="Title" %}
<h1>{{ currentCategoryTitle }}</h1>

Similarly, to get the category description, justnamethe parameter toDescription:

{# 获取ID为1的分类描述 #}
<p>分类描述:{% categoryDetail with name="Description" id="1" %}</p>

{# 获取当前分类描述并存储到变量 #}
{% categoryDetail currentCategoryDescription with name="Description" %}
<p>{{ currentCategoryDescription }}</p>

Get and display the category thumbnail

In AnQiCMS, category images are usually divided into 'large image' (Logo) and 'thumbnail' (Thumb).You can choose which image to display based on design requirements.

To get the large image (Logo) of the category, you can usename="Logo":

{# 获取ID为1的分类大图 #}
<img src="{% categoryDetail with name="Logo" id="1" %}" alt="{% categoryDetail with name="Title" id="1" %}"/>

{# 获取当前分类大图并存储到变量 #}
{% categoryDetail categoryLogo with name="Logo" %}
<div class="category-banner-image" style="background-image: url('{{ categoryLogo }}');"></div>

To get the category thumbnail (Thumb), usename="Thumb":

{# 获取ID为1的分类缩略图 #}
<img src="{% categoryDetail with name="Thumb" id="1" %}" alt="{% categoryDetail with name="Title" id="1" %}"/>

{# 获取当前分类缩略图并存储到变量 #}
{% categoryDetail categoryThumb with name="Thumb" %}
<img src="{{ categoryThumb }}" alt="分类缩略图"/>

Get and display the category Banner image (gallery)

Category banners are usually a set of images used for scrolling at the top of the category page, AnQiCMS names itImages. Since it may contain multiple images, it needs to be combinedforLoop to traverse and display.

To get the Banner group image of the category, you need to assignImagesto a variable and then loop:

{# 获取当前分类的Banner组图,并存储到categoryImages变量中 #}
{% categoryDetail categoryImages with name="Images" %}

{# 检查是否存在图片,然后循环展示 #}
{% if categoryImages %}
<div class="category-slider">
    {% for image in categoryImages %}
    <img src="{{ image }}" alt="分类Banner图"/>
    {% endfor %}
</div>
{% endif %}

If your design only needs the first image in the Banner group as the category header image, you can handle it like this:

{% categoryDetail bannerImages with name="Images" %}
{% if bannerImages %}
  {# 将组图的第一张图片赋值给pageBanner变量 #}
  {% set pageBanner = bannerImages[0] %}
  <img src="{{ pageBanner }}" alt="分类头图"/>
{% else %}
  {# 如果没有Banner图,可以显示一个默认图片 #}
  <img src="/static/images/default_banner.jpg" alt="默认Banner"/>
{% endif %}

Obtaining other practical fields

In addition to the above commonly used fields,categoryDetailTags can obtain more classification-related information, enhancing the flexibility and richness of the template:

  • Category content (Content): If you have filled in rich text content for categories in the background, you can usename="Content"Get. It should be noted that if the content contains HTML tags, it is usually necessary to配合|safeThe filter outputs safely, and if the Markdown editor is enabled on the backend, the content will automatically be converted to HTML.
  • Category link (Link): Passname="Link"You can directly obtain the access URL of the category, which is convenient for building navigation and links.
  • Number of documents (ArchiveCount): Use.name="ArchiveCount"You can obtain the total number of documents included in the category, often used to display "Total X articles."}
  • Parent category ID (ParentId): Passname="ParentId"Get, it can help you build more complex hierarchical relationship judgments in the template.
  • Custom fieldIf your content model is categorized with additional custom fields (such as "category icon", "custom banner description", etc.), you can also directly use these field names asnameRetrieve the value of the parameter. For example, if you have a custom field named 'Custom Banner Description', you can access it by{% categoryDetail with name="自定义横幅描述" %}Get it.

BycategoryDetailLabel, you can easily display the category information configured in the background to the user, creating a feature-rich and detailed website interface.


Frequently Asked Questions (FAQ)

  1. Question:categoryDetailWill the tag automatically obtain the category information of the current page?Answer: Yes, when you use the tag on any category detail page or category list pagecategoryDetailif not explicitly specifiedidortokenThe parameter will intelligently recognize and retrieve the category data of the current page, thereby simplifying your template code.

  2. How do I get the banner group picture of a certain category, but my design only needs to display the first picture as the category header?You can first go through{% categoryDetail bannerImages with name="Images" %}Get the Banner group image into a variablebannerImagesdue tobannerImagesis an array, you can use{% if bannerImages %}{% set pageBanner = bannerImages[0] %}{% endif %}In this way, the first element of the array (i.e., the first picture) is assigned to a new variablepageBannerAfter that, you can use it directly{{ pageBanner }}to display the first picture.

  3. Ask: If I add a custom field named "Category Icon" in the AnQiCMS backend for categories, how should I get and display it throughcategoryDetailthe tag?Answer: AnQiCMS will directly use the fields you customize for categories asnameavailable values for the parameters. Therefore, you can directly use{% categoryDetail with name="分类图标" %}Get the value of this custom field. If the field stores an image URL, you can wrap it with<img>tags to display.

Related articles

How to use the `categoryList` tag to build a multi-level category navigation or display category lists in the content area?

In Anqi CMS, properly organizing website content categories is an important aspect for improving user experience and website maintainability.Whether it is to build a clear multi-level navigation menu, help users quickly locate information, or skillfully display different categories of content in the page content area, the `categoryList` tag is the core tool for realizing these functions. ### Core Tag: `categoryList` Description The `categoryList` tag is used specifically in the Anqi CMS template engine to retrieve the website category list.

2025-11-08

How do the `prevArchive` and `nextArchive` tags display the link and title of the previous/next document on the document detail page?

When we browse content on the website built with AnQiCMS, carefully crafted documents bring value to readers.It is particularly important to provide 'Previous' and 'Next' navigation links at the end of each article to make the reader's reading experience more smooth and encourage them to explore more relevant content.This can effectively guide users to deeply browse within the website, increase page visits and user stickiness, and is also very beneficial for the internal link structure and SEO optimization of the website.

2025-11-08

How to use the `archiveDetail` tag to retrieve and fully display the title, content, and custom fields of a specific document?

In AnQiCMS, displaying the detailed content of a single document is one of the core requirements for website construction.Whether it is an article detail page, a product detail page, or other custom type of content display, we need a flexible and efficient way to obtain and present the title, body of the document, and custom fields defined according to business requirements.The `archiveDetail` tag was born for this purpose, it can help you easily achieve this goal.### Deeply understand the `archiveDetail` tag

2025-11-08

How to accurately control the display quantity, sorting, and filtering conditions of the `archiveList` tag?

In Anqi CMS, the `archiveList` tag is a core tool for dynamically calling and displaying document lists.Whether it is a blog article, product showcase, or news update, this tag can help you flexibly control the display quantity, sorting method, and various filtering conditions according to your specific needs.A deep understanding of its parameters will enable you to build a highly customized and powerful content display page.

2025-11-08

How to effectively manage and display single-page content on a website using `pageList` and `pageDetail` tags?

In website operation, single-page content plays an indispensable role, it usually carries important information such as "About Us", "Contact Us", "Terms of Service", "Privacy Policy", etc., which needs to be clearly displayed and also requires convenient management.AnQiCMS provides the powerful template tags `pageList` and `pageDetail`, which help us efficiently manage and display these single-page content.

2025-11-08

How to display all related document lists under a specific `tagDataList` tag and support pagination?

In AnQi CMS' daily content management, we often need to organize and display website content more flexibly.In addition to the traditional classification system, tags (Tag) provide us with another powerful way to associate content.It can aggregate documents from different categories but with similar themes, providing users with a more focused browsing experience.Today, let's delve into a very practical template tag in Anqi CMS, `tagDataList`, and see how it helps us display all related document lists under a specific tag and easily implement pagination.

2025-11-08

How to use the `navList` tag to create the main navigation menu of a website and support two-level dropdown display?

The main navigation menu of the website is an important entry for users to understand the content and structure of the website.The Anqi CMS provides a powerful and flexible `navList` tag, which helps us easily create and manage multi-level dropdown navigation menus, thus enhancing the user experience and clarity of the information architecture of the website. ### Step 1: Configure the navigation menu in the AnQi CMS backend Before starting to write template code, we need to set up the structure of the navigation menu in the AnQi CMS backend management system. 1.

2025-11-08

How to generate `breadcrumb` tags?

On a day when the content of a website is becoming richer, users often need to navigate between different pages on the website.To help visitors understand their current location and provide a convenient return path, breadcrumb navigation (Breadcrumb Navigation) has emerged.It is like a path indicator on a map, making it clear for the user to know 'Where I came from and where I am going to.'

2025-11-08