As an experienced CMS website operator, I am well aware of the importance of high-quality, easily accessible content information for website user experience and search engine optimization.In the Anqi CMS, obtaining the detailed information of the current visited category is a very basic and frequent requirement in website construction. Whether it is used for page titles, navigation breadcrumbs, or detailed introductions on category pages, it is necessary to accurately call these data.
AutoCMS provides us with a powerful and flexible template tag system, making it extremely easy to obtain the title, link, description, and even custom fields of the current visited category.
Get the basic information of the current access category
In the template of Anqi CMS, obtaining detailed information of the current category mainly depends oncategoryDetailLabel.The strength of this tag lies in its ability to automatically identify the category of the current page when you do not need to specify a category ID or alias, and extract the required data from it.This greatly simplifies the complexity of calling category information on the category page or document detail page.
For example, in the category list page (such as{模型table}/list.htmlor{模型table}/list-{分类ID}.html) or document detail page (such as{模型table}/detail.htmlor{模型table}/detail-{文档ID}.html)categoryDetailTags can intelligently obtain the classification data in the current context.
Title (Title)
We can use to display the title of the current visited classification.categoryDetaila tag to specifynameparameter as"Title"This title is usually used for the main title of category pages, or as the text of category links on document detail pages.
<div>当前分类标题:{% categoryDetail with name="Title" %}</div>
Link (Link)
It is crucial to obtain the current category URL link for constructing navigation, breadcrumbs, and optimizing internal link structure. By usingnameparameter settings"Link",categoryDetailtags, the complete link of the category will be returned.
<a href="{% categoryDetail with name="Link" %}">前往此分类页面</a>
Description and Content
The description of a category is usually a brief text summary, commonly used for page meta descriptions (meta description) or as an introduction in category lists.ContentThe field can carry more rich text content, used for detailed introductions on category pages.
To get the category description:
<div>分类描述:{% categoryDetail with name="Description" %}</div>
To get the detailed content of the category:
<div>分类详细内容:{% categoryDetail categoryContent with name="Content" %}{{categoryContent|safe}}</div>
Please note that whenContentField contains HTML or other rich text format, in order to ensure that the content is rendered correctly and not automatically escaped, we usually need to use|safeFilter. If the category content is written using Markdown, the CMS will default to converting it to HTML, and you can alsorender=trueorrender=false参数来手动控制是否进行Markdown渲染。
Get Custom Field
The strength of AnQi CMS lies in its flexible content model, allowing operations personnel to add various custom fields to categories.These custom fields greatly expand the expressiveness of category information, for example, you can add special icons, color codes, additional introduction text, or unique image sets.
To get the specific custom field of a category, if you know the name of the field (referred to as the 'call field' defined in the backend content model), you can directly access it throughnameParameters are called. For example, if you define a custom field namedcustomIconwith a name:
<div>自定义图标:<img src="{% categoryDetail with name="customIcon" %}" alt="分类图标" /></div>
In addition to directly calling specific fields, the Aanqi CMS also provides a method to display all custom fields by iteration.This is very useful for scenarios that need to dynamically display all the additional attributes of categories.name="Extra"We can get an array of all custom fields, then useforLoop through them.
{% categoryDetail extras with name="Extra" %}
{% for field in extras %}
<div>{{field.Name}}:{{field.Value}}</div>
{% endfor %}
field.NameIt will display the Chinese name of the field (or the parameter name you set in the background), whilefield.Valueit will display the corresponding value.
Additionally, the classification supports some built-in 'custom' image fields, such asLogofor the main image,Thumbfor the thumbnail, as well asImagesUsed for multiple banner images. The calling method of these fields is similar to that of general custom fields.
<div>分类主图:<img src="{% categoryDetail with name="Logo" %}" alt="{% categoryDetail with name="Title" %}" /></div>
IfImagesThe field contains multiple images and usually needs to be配合forto display in a loop:
{% categoryDetail bannerImages with name="Images" %}
{% if bannerImages %}
<div class="category-banners">
{% for img in bannerImages %}
<img src="{{img}}" alt="{% categoryDetail with name="Title" %} banner" />
{% endfor %}
</div>
{% endif %}
Through the above method, the operation personnel of Anqi CMS can easily and flexibly obtain and display all the detailed information of the current visit category, thereby creating a content-rich, well-structured, and user-friendly website page.The mastery of these abilities is crucial for enhancing the effectiveness of content marketing and the overall competitiveness of the website.
Frequently Asked Questions
Q1: How do I get the details of a specific category that is not currently visited?
A: If you need to get the details of a specific category that is not currently visited on the page,categoryDetailTags throughidortokenThe parameter specifies the target category.idThe parameter accepts the numeric ID of the categorytokenThe parameter accepts the URL alias of the category. For example, to get the title of the category with ID 10:{% categoryDetail with name="Title" id="10" %}; or through the aliasabout-usGet category link:{% categoryDetail with name="Link" token="about-us" %}.
Q2: Does the category content support Markdown format? How to control rendering?
A: Yes, if you have enabled the Markdown editor in your security CMS backend content settings, and the category content is written using Markdown,categoryDetailTagsContentThe field will automatically convert Markdown to HTML. If you need to manually control the rendering process, you can addContentthe field.renderparameter. Set it torender=trueCan enforce Markdown conversion, andrender=falsewill prevent conversion and output the original Markdown text directly. Please be sure to cooperate with|safeThe filter to ensure the correct display of HTML content.
Q3: If I need to display a list of categories instead of the details of a single category, which tag should I use?
A: For displaying category lists, you should usecategoryListtags. This tag allows you to filter by multiple conditions (such asmoduleIdto get categories under a specified content model,parentIdto get child categories or top-level categories,limitControl the quantity and others) to get multiple categories. It will return an array of category objects, and you need to iterate through this array, and then from each category itemforLoop through this array, and then from each category itemitemExtract the title, link, and description information from it. For example, to get the top-level category list of the article model:{% categoryList categories with moduleId="1" parentId="0" %}{% for item in categories %}<a href="{{item.Link}}">{{item.Title}}</a>{% endfor %}{% endcategoryList %}.