As a senior website operation expert, I know how important it is to flexibly obtain and display data when managing content.Especially when building the AnQiCMS website, we often encounter situations where we need to obtain specific category information rather than the current page context.Today, let's delve into how to skillfully use the template tags of AnQiCMS to accurately obtain the category details you want.
English: How to accurately locate the category details of a specified ID rather than the current page?
In the template development of AnQiCMS,categoryListandcategoryDetailThese are two very commonly used classification-related tags.Beginners may often confuse their functions or be unclear about how to implement the specific requirement of 'retrieving details of a specified ID category'.The core lies in understanding the different emphases of these two tags and learning to use their parameters flexibly.
Understanding the category tag system of AnQiCMS
AnQiCMS's template system follows the syntax style of Django, which is simple and powerful. It provides two main tags for category information calls:
categoryListTags:As the name implies, this tag is mainly used to obtainCategory list. When you need to display multiple categories on a page, such as the category navigation in the sidebar, the category module list on the homepage,categoryListIt is your first choice. It will return an array of category objects, you can iterate through it to display the title, link, and other information of each category.categoryListTags have some parameters, such asmoduleId(Specify the model ID),parentIdauto for specifying the parent category ID, used for filtering and organizing the list. It is worth noting that if no explicit specification is madecategoryListautocategoryIdParameter, it indeed tries to read the category ID of the current page to filter the content. But if you want to get the list of categories unrelated to the current page,specific categoriesYou can explicitly setparentId="0"to get the top-level categories, or bycategoryIdspecifying the category set you want with thecategoryListis designed to handlemultiple categoriesthe set, if you just want todetails of a specific category, it is not the most direct and efficient tool.categoryDetailTags:this is how to getdetails of a single specified categoryEnglish translation of 'auto' to 'English': The 'about us' category with ID 5, or the 'product case' category with ID 10, when you know which category you want to display (for example), and you want to get its detailed attributes such as title, description, link, image, etc., you should usecategoryDetail。It is designed to directly return all available fields of a classified object, allowing you to accurately extract the required information.
Core solution: Make good use ofcategoryDetailofidParameters
To implement 'Get the category details of a specified ID instead of the current page', we should focus oncategoryDetailthe label and pass itidthe parameter to specify the target category accurately.
categoryDetailThe basic usage of the tag is as follows:
{% categoryDetail 变量名称 with name="字段名称" id="指定分类ID" %}
Here are some key points:
id="指定分类ID"This is the core to achieve the goal. By assigningida specific category number ID to the parameter (e.g.,)id="5"You can tell AnQiCMS that, regardless of which category the current page is, I only want the details of the category with ID 5.This completely avoids the context of "current page".name="字段名称"This is used to specify a specific field of the category you want to retrieve, such as"Title"(Title),"Link"(Link),"Description"(description),"Logo"Thumbnail full-size and others. If you need to get multiple fields, you can call it multiple times.categoryDetailOr, more recommended, is to set a variable name for the entire category object.变量名称English: Optional parameter, if you need to get the entire object of the category for future multiple references to its different fields, you can specify a variable name (such asmyCategory).
Example: Get details of the category with ID 5
假设我们想在页面的任意位置(比如网站的底部导航栏或者一个独立的推荐模块)展示ID为5的分类的标题和链接,而不受当前页面是哪个分类的影响。
<div class="specific-category-info">
<h3>了解我们</h3>
{% categoryDetail aboutUsCategory with id="5" %} {# 将ID为5的分类详情赋值给aboutUsCategory变量 #}
{% if aboutUsCategory %} {# 检查分类是否存在 #}
<p>分类ID: {{ aboutUsCategory.Id }}</p>
<p>分类名称: <a href="{{ aboutUsCategory.Link }}">{{ aboutUsCategory.Title }}</a></p>
<p>分类描述: {{ aboutUsCategory.Description }}</p>
{# 还可以获取更多字段,例如图片: #}
{% if aboutUsCategory.Logo %}<img src="{{ aboutUsCategory.Logo }}" alt="{{ aboutUsCategory.Title }}"/>{% endif %}
{% else %}
<p>指定ID的分类不存在。</p>
{% endif %}
</div>
在这个例子中,我们通过id="5"The parameter accurately specifies the category to be retrieved is the category with ID 5.Even if the current page is an article detail page, a product list page, or any other page, this code will independently search the database and display the category information with ID 5.
CombinecategoryListThe scene consideration
AlthoughcategoryDetailIs the **method to obtain the details of a single specified ID category, but in some special scenarios, you may first usecategoryListObtained a collection of categories, and then in the loop of this collection, used for a category that meets a specific conditioncategoryDetailGet more information (this is usually rare, becausecategoryListitself returnsitemthe object already includes most of the commonly used fields)
For example, if you want to iterate through all top-level categories, and for a specific category named "Hot Topics" (assuming its ID is 12), you want to additionally display a unique custom field for it, you can do it like this:
{% categoryList topCategories with moduleId="1" parentId="0" %}
<ul class="top-nav-categories">
{% for category in topCategories %}
<li>
<a href="{{ category.Link }}">{{ category.Title }}</a>
{% if category.Id == 12 %} {# 如果当前分类是ID为12的“热门专题” #}
<div class="featured-topic-extra">
{# 这里可以使用categoryDetail再次获取ID为12的分类,以访问其自定义字段,
尽管通常categoryList返回的item已经包含了大部分数据,但若要访问其独特且复杂或不直接在item中的字段,可以考虑。#}
{% categoryDetail featuredTopicDetails with id="12" %}
{% if featuredTopicDetails.CustomField %} {# 假设有一个名为CustomField的自定义字段 #}
<p>专题特色: {{ featuredTopicDetails.CustomField }}</p>
{% endif %}
{% endcategoryDetail %}
</div>
{% endif %}
</li>
{% endfor %}
</ul>
{% endcategoryList %}
But please note that in most cases,categoryListthe loop returns,itemthe object itself includes all standard fields and custom fields of the classification, directly throughitem.CustomFieldIt will call more efficiently, avoiding repeated queries. Only when you need to get one based on a certain condition (such as the current loop's category ID)completely different from the current loop categoryWhen specifying a specific category detail,categoryDetailwithidthe parameter will be validcategoryListin the context.
Summary and **Practice
In AnQiCMS, to getthe category detail of the specified ID,而不是当前页面的分类信息,最直接、最高效的方式就是使用 EnglishcategoryDetail标签,并精确地传入 Englishid参数来指定目标分类的数字ID。 English
categoryList:用于获取 EnglishListEnglish for displaying multiple categories scenarios.categoryDetail:用于获取 EnglishDetails of a single category.Throughidortoken