When operating the AnQiCMS website, we often need to dynamically display detailed information and descriptions of the current category on the category page, such as the title, introduction, large image, and even custom fields.This can not only help users better understand the theme of the current page, but also effectively improve the SEO performance of the page.The AutoCMS provides very flexible and intuitive template tags, making this task easy and simple.
KnowcategoryDetailTags: Your category information 'Smart Assistant'
The template system of Anqi CMS uses syntax similar to Django, and the core tool for obtaining category details iscategoryDetailLabel. You can imagine it as an intelligent assistant, you tell it what fields you want to see, and it can accurately extract the classification information of the current page.
In most cases, when you visit a category page, such as an article list page or a product list page,categoryDetailLabels will automatically identify the current category and extract the information you need from it. You usually don't need to specify the category ID separately; it can understand what you mean.
If indeed you need to retrieve *non-current page* specific category information, such as displaying the details of the "About Us" category in the sidebar, you can do so byidparameters ortokenThe parameter is used to specify explicitly, for example:{% categoryDetail with name="Title" id="10" %}.
How to display detailed information of each category?
categoryDetailThe usage of tags is usually:{% categoryDetail 变量名 with name="字段名称" %}.In which, "variable name" is optional, if you want to output the result directly, you can omit it; if you want to store the data in a variable for subsequent processing, you can define a variable name.nameThe parameter is the specific field you want to obtain.
Below are some commonly used fields, as well as their calling methods in the template:
Category Title (Title):This is the core display content of the category.
<h1>{% categoryDetail with name="Title" %}</h1>Category Link (Link):Used to generate hyperlinks pointing to the current category page.
<a href="{% categoryDetail with name="Link" %}">更多内容</a>分类描述 (Description) 和 分类内容 (Content):
Description通常是简短的分类摘要,适合用于metaLabel or the introduction at the top of the page.ContentIt is a more detailed category introduction, which may contain rich text content.<p>{% categoryDetail with name="Description" %}</p> <div> {# 注意:Content字段可能包含HTML,需要使用|safe过滤器确保正确渲染 #} {%- categoryDetail categoryContent with name="Content" %} {{ categoryContent|safe }} </div>Here we especially remind you,
ContentIf the field contains HTML tags, it must be added.|safeFilter, otherwise the HTML code will be escaped instead of rendered as page structure.Image information (Logo, Thumb, Images):
Logo:通常指分类的大图或主图。Thumb:指分类的缩略图。Images:如果分类设置了多张Banner图或组图,可以使用ImagesField gets an image array and displays it in a loop.
{# 显示分类Logo大图 #} <img src="{% categoryDetail with name="Logo" %}" alt="{% categoryDetail with name="Title" %}" /> {# 显示分类Banner组图(如果有) #} {% categoryDetail categoryBanners with name="Images" %} {% if categoryBanners %} {# 检查是否有图片存在,避免空循环 #} <div class="category-banner-slider"> {% for imgUrl in categoryBanners %} <img src="{{ imgUrl }}" alt="{% categoryDetail with name="Title" %}" /> {% endfor %} </div> {% endif %}Parent category ID (ParentId) and document count (ArchiveCount):
ParentIdIt can help you build more complex classification navigation, such as displaying the parent classification name of the current classification.ArchiveCountIt can let you know how many articles or products are under the current classification.{# 获取上级分类ID,并尝试获取其标题 #} {% set currentParentId = "" %} {% categoryDetail parentId with name="ParentId" %}{% set currentParentId = parentId %} {% if currentParentId and currentParentId != "0" %} <p>上级分类:<a href="{% categoryDetail with name="Link" id=currentParentId %}">{% categoryDetail with name="Title" id=currentParentId %}</a></p> {% endif %} <p>包含文章数:{% categoryDetail with name="ArchiveCount" %}</p>
Process custom category field
The "Flexible Content Model" of AnQi CMS is a very powerful feature, allowing you to add exclusive fields for different content types (such as articles, products, and custom models you define). If you also add custom fields to the category model, you can also do so throughcategoryDetailyou can get it by tag.
If you add a category with a name ofcustom_banner_textyou can call it directly:
<p>自定义Banner文本:{% categoryDetail with name="custom_banner_text" %}</p>
If you want to loop through all custom fields, you can usename="Extra":
{% categoryDetail extras with name="Extra" %}
{% for field in extras %}
<div>
<strong>{{ field.Name }}:</strong>
{# 注意:自定义字段的值也可能包含HTML,酌情使用|safe #}
<span>{{ field.Value|safe }}</span>
</div>
{% endfor %}
Combine an example: Display detailed information on the category list page
Let's look at a complete example of displaying the current category details in a category list template. Suppose this isarticle/list.htmlTemplate:
{% extends 'base.html' %} {# 继承基础布局 #}
{% block main_content %}
<div class="category-header">
{# 显示当前分类的大标题 #}
<h1>{% categoryDetail with name="Title" %}</h1>
{# 如果有分类Logo,显示它 #}
{% categoryDetail categoryLogo with name="Logo" %}
{% if categoryLogo %}
<img src="{{ categoryLogo }}" alt="{% categoryDetail with name="Title" %}" class="category-logo" />
{% endif %}
{# 显示分类的描述 #}
<p class="category-description">{% categoryDetail with name="Description" %}</p>
{# 显示分类的详细内容,并确保HTML正确渲染 #}
<div class="category-full-content">
{%- categoryDetail categoryContent with name="Content" %}
{{ categoryContent|safe }}
</div>
{# 假设你有一个自定义字段 "category_slogan" #}
{% categoryDetail categorySlogan with name="category_slogan" %}
{% if categorySlogan %}
<p class="category-slogan">“{{ categorySlogan }}”</p>
{% endif %}
</div>
<div class="article-list">
<h2>此分类下的文章</h2>
{# 获取并展示当前分类下的文章列表 #}
{% archiveList articles with type="page" limit="10" %}
{% for article in articles %}
<div class="article-item">
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<p>{{ article.Description }}</p>
<span>发布日期:{{ stampToDate(article.CreatedTime, "2006-01-02") }}</span>
<span>阅读量:{{ article.Views }}</span>
</div>
{% empty %}
<p>当前分类下还没有文章。</p>
{% endfor %}
{# 分页导航 #}
{% pagination pages with show="5" %}
{# 这里可以插入你的分页代码 #}
{% endpagination %}
{% endarchiveList %}
</div>
{% endblock %}
Through this example, we can see how to getcategoryDetailthe information obtained by tags, andarchiveListother tags combined, together constructing a content-rich and feature-complete classification page.
Summary
Anqi CMS'scategoryDetailTags are an indispensable tool for website content operators and template developers.It provides a high degree of control over category details in a concise manner, whether it's the basic title, description, or complex multi-image carousel and custom fields, all can be easily achieved.Use these tags to help enhance the information display and user experience of your website.
Common Questions (FAQ)
1. How to get the name and link of the parent category on the current category page?
You first need to get the current category'sParentId. Then, you can use it again withcategoryDetaillabel, passing thisParentIdGet the detailed information of the parent category.
`twig {% categoryDetail currentCategory with name=“Id” %} {# 获取当前分类ID #} {% categoryDetail parentId with name=“ParentId” id=currentCategory %} {# 获取当前分类的父ID #}
{% if parentId and parentId != "0" %}
<p>父级分类:<a href="{% categoryDetail with name="Link" id=parentId %}">{% categoryDetail with name="Title" id=parentId %}</a></p>
{% else %} English
<p