How to delve deeper into the details of the AnQi CMS document detail page to obtain all the information of the category it belongs to?
Understanding the context of the document detail page
When a user visits a document page (such as an article or a product detail), the Anqi CMS template system will load the corresponding document detail page template. In this template, all the information of the current document has been presented in aarchiveIn the form of an object, or byarchiveDetailTags that you can call at any time. The key to obtaining the information of its category lies in using thisarchiveThe category ID contained in the object.
The most direct way: usearchive.Categoryobject
AnQi CMS took full consideration of content relevance during design, therefore, in the document detail page, it is usually possible to directly througharchiveAn object accesses its category details. This means that the document object itself may already carry the complete data of its category.
Assuming you have already obtained the current document in the template'sarchiveobject (which is usually available by default on the document details page, or you can get one named by{% archiveDetail currentArchive %}the tagcurrentArchiveThe document object, you can directly accesscurrentArchive.Categoryto access the various fields of the category.
For example, if you want to display the title, link, and description of the category, you can operate like this:
{# 假设当前文档对象已命名为 'archive' 或 'currentArchive' #}
{% if archive.Category %}
<p>所属分类:<a href="{{ archive.Category.Link }}">{{ archive.Category.Title }}</a></p>
<p>分类描述:{{ archive.Category.Description }}</p>
{# 如果分类有缩略图或Logo,也可以直接调用 #}
{% if archive.Category.Logo %}
<img src="{{ archive.Category.Logo }}" alt="{{ archive.Category.Title }}" />
{% endif %}
{% endif %}
This method is very concise and efficient, it saves the additional tag call and parameter passing, and is the most recommended way to obtain information about the document's category.
Precise query and customization: usingcategoryDetailTag
Althougharchive.CategoryProvided great convenience, but in some cases, you may need to finely control which classification fields to obtain, or you want to obtain the current document classification ofCustom fieldThis is where the AnQi CMS providedcategoryDetaillabels can be put to use.
categoryDetailThe label allows you to accurately query the details of the category based on the category ID. And this category ID, we can also obtain from the current document'sarchive.CategoryIdfield.
Its basic usage method is{% categoryDetail 变量名称 with id="分类ID" %}.
For example, if you want to get the title of the category of the current document and the custom "category banner image" field (assuming you have added a category model named in the background)banner_imageYour custom field
{# 假设当前文档对象为 'archive' #}
{% if archive.CategoryId %}
{% set currentCategoryId = archive.CategoryId %} {# 将分类ID赋值给一个变量,方便后续使用 #}
<p>通过categoryDetail获取分类标题:{% categoryDetail with name='Title' id=currentCategoryId %}</p>
<p>通过categoryDetail获取分类链接:<a href="{% categoryDetail with name='Link' id=currentCategoryId %}">查看更多</a></p>
{# 进一步获取分类的自定义字段,例如“分类横幅图” #}
{% categoryDetail categoryBanner with name='banner_image' id=currentCategoryId %}
{% if categoryBanner %}
<img src="{{ categoryBanner }}" alt="{% categoryDetail with name='Title' id=currentCategoryId %} 的横幅图" />
{% endif %}
{# 如果您想一次性获取分类的所有自定义字段并遍历显示 #}
{% categoryDetail categoryExtras with name="Extra" id=currentCategoryId %}
{% if categoryExtras %}
<h4>分类自定义属性:</h4>
<ul>
{% for field in categoryExtras %}
<li>{{ field.Name }}:{{ field.Value }}</li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
In the above example, we first go through{% set currentCategoryId = archive.CategoryId %}Store the current document's category ID in a temporary variable. Then, we can use it repeatedly.categoryDetaillabel and pass thisidparameter to retrieve the required category information, including the standardTitle/Linkas well as throughname='banner_image'orname='Extra'custom field obtained.
Comprehensive application example: building a complete classification information block
Now, let's integrate these knowledge points to build a block that displays the complete information of the category on the document detail page.
`twig
{# 获取当前文档对象 #}
{% archiveDetail currentArchive %}
<header>
<h1>{{ currentArchive.Title }}</h1>
<div class="meta-info">
发布于:{{ stampToDate(currentArchive.CreatedTime, "2006年01月02日") }} | 浏览量:{{ currentArchive.Views }}
</div>
</header>
<section class="category-context">
<h2>所属分类信息</h2>
{% if currentArchive.Category %}
<div class="category-card">
<a href="{{ currentArchive.Category.Link }}" class="category-title">
<h3>{{ currentArchive.Category.Title }}</h3>
</a>
{% if currentArchive.Category.Logo %}
<img src="{{ currentArchive.Category.Logo