How does the `categoryDetail` tag help us retrieve and display detailed information for a specific category?

AnQiCMS (AnQiCMS) provides powerful and flexible tools for website content management, includingcategoryDetailTags play a very important role in building a website that is rich in content and clear in structure.It can help us accurately obtain and display detailed information about a specific category, whether it is used for the header introduction on the category list page or to display more content about the category on the article detail page, it is very convenient.

categoryDetailBasic usage of tags

In simple terms,categoryDetailThe label's function is to "get detailed information about a category".This is similar to the logic of obtaining article details or single-page details, it allows us to specify a category, then extract the title, description, link, image, and all related data of the category, and display them on the website front end.

Typical application scenarios of this tag include:

  • Display the current category title, SEO description, and image at the top of the category list page.
  • Show the category name and link on the article or product detail page, for easy user navigation.
  • In the sidebar or footer navigation of the website, dynamically display information for specific categories.

How to usecategoryDetailTag

categoryDetailThe basic syntax structure of the tag is:{% categoryDetail 变量名称 with name="字段名称" %}.

  • 变量名称This is an optional parameter. If we need to further process the obtained category information in the template, or want to assign the entire category object to a variable, then we can define a variable name.If you do not need to handle it specially, you can also omit the variable name when directly outputting the value of a field.
  • name="字段名称": This is the most core parameter, it tells the tag which specific field of the category we need to obtain. For example,name="Title"Will retrieve the category title,name="Description"Will retrieve the category description.

In addition to specifying the fields to be retrieved, we can also use other parameters to clarify which category it is:

  • id="分类ID"Through specifying the numeric ID of the category, you can obtain its details. This is the most direct and commonly used method. If not specifiedid,categoryDetailThe tag will default to trying to obtain the information of the category the current page belongs to.
  • token="分类URL别名"If the category is set to a custom URL alias (usually English or pinyin), you can also specify the category through this alias.
  • siteId="站点ID"In the multi-site management scenario of AnQiCMS, if you need to obtain the category information of other sites, you can throughsiteIdSpecify the target site with parameters

Common fields and their application examples

categoryDetailThe tag can retrieve various information about the category, and below are some commonly used fields and their application in the template:

  1. Category ID (Id) and category title (Title)This is the most basic classification information, often used to display classification names or as a basis for internal logic judgments.

    <!-- 获取当前页面的分类ID -->
    <div>当前分类ID:{% categoryDetail with name="Id" %}</div>
    <!-- 获取ID为5的分类标题 -->
    <div>特定分类标题:{% categoryDetail with name="Title" id="5" %}</div>
    
  2. Category link (Link)Used to generate a category page jump link, ensuring that users can click to visit the detailed content of the category.

    <!-- 创建一个指向当前分类的链接 -->
    <a href="{% categoryDetail with name="Link" %}">
        {% categoryDetail with name="Title" %}
    </a>
    
  3. Category description (Description) and category content (Content) DescriptionIt is usually used for SEO optimization or a brief category introduction, whileContentit is used to display the main content of the category page. IfContentThe field contains Markdown formatted content, and the background has enabled Markdown editor, which will automatically render it as HTML; if you want to manually control the rendering behavior, you can userenderparameters, for examplename="Content" render=true.

    <!-- 显示分类的SEO描述 -->
    <meta name="description" content="{% categoryDetail with name="Description" %}">
    <!-- 显示分类页面的主体内容,并确保HTML安全渲染 -->
    <div class="category-content">
        {% categoryDetail categoryFullContent with name="Content" %}
        {{ categoryFullContent|safe }}
    </div>
    
  4. Parent category ID (ParentId)When building a multi-level category navigation or breadcrumb navigation, this field is very useful, as it can be used to determine the hierarchy of the current category.

    <!-- 获取当前分类的上级分类ID -->
    {% categoryDetail currentCategoryParentId with name="ParentId" %}
    {% if currentCategoryParentId > 0 %}
        <div>上级分类ID:{{ currentCategoryParentId }}</div>
    {% endif %}
    
  5. Category thumbnail (Thumb) and category large image/Logo (Logo)Categorization images are usually used for displaying category lists or as the top banner image on category pages.LogoIt usually refers to the original or larger image,Thumbwhich is a thumbnail processed by the system.

    <!-- 在分类列表或详情页显示分类缩略图 -->
    <img src="{% categoryDetail with name="Thumb" %}" alt="{% categoryDetail with name="Title" %}">
    
  6. Category Banner group image (Images)This field is very suitable for displaying a carousel or multiple promotional images at the top of the category page. It should be noted that,ImagesIt returns an array of image URLs, so it needs to be配合forLoop to traverse and display.

    {% categoryDetail categoryBanners with name="Images" %}
    {% if categoryBanners %}
        <div class="category-banner-slider">
            {% for imageUrl in categoryBanners %}
                <img src="{{ imageUrl }}" alt="分类横幅">
            {% endfor %}
        </div>
    {% endif %}
    
  7. Number of categorized documents (ArchiveCount)You can display how many articles or products are under the category name next to it, improving the information volume and user experience.

    <h3>
        <a href="{% categoryDetail with name="Link" %}">
            {% categoryDetail with name="Title" %} (共{% categoryDetail with name="ArchiveCount" %}篇文章)
        </a>
    </h3>
    
  8. Custom fieldIf we set custom fields for categories in the content model of the backend, these fields can also be accessed throughcategoryDetailtags. You can access them directly throughname="你的自定义字段名"this method.

    <!-- 如果有一个名为 "category_slogan" 的自定义字段 -->
    <p>分类口号:{% categoryDetail with name="category_slogan" %}</p>
    
    <!-- 遍历所有自定义字段 -->
    {% categoryDetail extras with name="Extra" %}
    {% for field in extras %}
        <div>{{ field.Name }}:{{ field.Value }}</div>
    {% endfor %}
    

Summary

categoryDetailTags are an indispensable part of AnQiCMS template development, making it intuitive and efficient to obtain and display classification information.By flexibly utilizing the various parameters and fields it provides, we can easily build dynamic, user-friendly category pages, thereby effectively enhancing the organization and SEO performance of the website's content.Whether it is a simple title display, or a complex picture carousel and custom field display,categoryDetailAll of them can provide strong support.

Frequently Asked Questions (FAQ)

1. Why mycategoryDetailThe label does not display any content?

The most common reason is that you have not specifiedidortokenOr the current page is not associated with any category, causing the tag to be unable to automatically identify the category to be retrieved.Make sure to specify the category ID or alias you want to use when operating, or use it on the category page so that it can automatically obtain information about the current category.Also, also checknameIs the parameter spelled correctly, and does the category indeed exist for the field data you are trying to retrieve?

2. How tocategoryDetailHow to get all custom fields of a specific category instead of writing them out one by one?

You can usename="Extra"To get an array containing all custom fields. Then, you can go throughforLoop through this array to display the names and values of each custom field. For example:{% categoryDetail extras with name="Extra" %}{% for field in extras %} <div>{{ field.Name }}:{{ field.Value }}</div>{% endfor %}.

3.LogoandThumbWhat are the differences between the fields? Which one should I use?

LogoIt usually refers to the original or larger-sized images used for the main visual or large banners on category pages.ThumbIt is usually a small thumbnail image automatically generated by the system according to preset rules, suitable for category lists, card displays, or where page loading resources need to be saved. You should choose to use it according to your design requirements and the use scenario of the image.LogoOrThumb.