In the daily operation of AnQi CMS, content classification is a key factor in organizing information, enhancing user experience, and optimizing SEO.Whether it is to generate breadcrumb navigation for articles, or to display other contents under the current category in the sidebar, or to set dynamic titles for category pages, obtaining the ID and name of the current document's category is a common requirement in template development.The AnQi CMS provides intuitive and powerful template tags, allowing you to easily call this information on the front-end page.
This article will delve into how to obtain the ID and name of the current document's category in AnQi CMS in a natural and smooth manner, and provide practical code examples to help you better manage and present website content.
Understand the template tag system of AnQi CMS
The template system of AnQi CMS adopts a syntax style similar to Django, with the core being the use of double curly braces{{ 变量 }}Output variable content, as well as using single curly braces with the percentage sign{% 标签 %}Call the functional tag. This design makes the template code easy to read and powerful, allowing the website content to be displayed dynamically according to business logic.
To obtain information about the category of the current document, we will mainly usearchiveDetailandcategoryDetailthese two core tags.
Directly obtain the ID and name of the category of the current document
When we are on a document detail page, Anqi CMS usually loads the complete data of the current document into the page context and names itarchiveObject. Taking advantage of this, we can directly access its classification information.
1. Get the classification ID
To obtain the unique identifier ID of the category to which the current document belongs, you can directly accessarchivethe object'sCategoryIdproperty. This is a numeric ID, the key to the associated category.
{# 假设当前页面上下文已存在 archive 对象,通常在文档详情页会自动提供 #}
{% if archive.CategoryId %}
<p>当前文档所属分类ID:{{ archive.CategoryId }}</p>
{% else %}
<p>当前文档未指定分类或分类信息不可用。</p>
{% endif %}
2. Get the category name and more information (via category object)
One point that Anqi CMS is more convenient is,archiveThe object not only contains the category ID, but also provides a completeCategoryThe child object encapsulates the name, link, description, and other commonly used attributes of the category. This means that you do not need to query again and can directly obtain the title and link of the category.
{% if archive.Category %}
<p>文档所属分类名称:{{ archive.Category.Title }}</p>
<p>分类链接:<a href="{{ archive.Category.Link }}">{{ archive.Category.Link }}</a></p>
<p>分类ID:{{ archive.Category.Id }}</p>
{# 您还可以获取更多分类属性,例如 Description #}
<p>分类描述:{{ archive.Category.Description }}</p>
{% else %}
<p>无法获取文档的分类对象信息。</p>
{% endif %}
This method is the most direct and efficient because it avoids additional database queries and uses the data already present on the current page directly.Under most document detail page scenarios, this is the most recommended way to obtain category information.
UtilizearchiveDetailCombinecategoryDetailExercise finer control
Although direct accessarchive.CategoryObjects are very convenient, but in certain specific scenarios, you may need to obtain classified data more flexibly, such as:
- You are not on the document detail page, but you have the document ID and need to retrieve its classification information.
archive.CategoryThe object does not contain the custom classification field you need.- You can find the brother or child categories of the category through the category ID of the document.
At this time, we can combine to usearchiveDetailandcategoryDetail.
1. UsearchiveDetailExplicitly obtain the category ID of the current document
Even ifarchive.CategoryIdHas been in context, passedarchiveDetailExtracting the classification ID explicitly is also a clear practice, especially whenarchiveThe object is not in the global context.
{# 使用 archiveDetail 标签明确地提取当前文档的分类ID,并赋值给 docCategoryId 变量 #}
{% archiveDetail docCategoryId with name="CategoryId" %}
<p>通过 archiveDetail 获取的分类ID: {{ docCategoryId }}</p>
{% endarchiveDetail %}
2. Pass the obtained classification ID tocategoryDetail
Once we have the category ID (whether obtained througharchive.CategoryIddirectly or througharchiveDetaillabel extraction), we can pass it as a parameter tocategoryDetailthe label to obtain any details about the category.
categoryDetailThe tag allows you to pass `