How to get the category details information of a specified ID through the `categoryList` tag instead of the current page?

As an experienced website operations 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 AnQiCMS template tags to accurately obtain the category details you want.

How to accurately locate: How to obtain the category details of a specified ID, rather than the current page?

In AnQiCMS template development,categoryListandcategoryDetailAre two very commonly used classification-related tags. Beginners may often confuse their functions or be unclear about how to implement the specific requirement of "getting the details of a specified ID category."}The essence lies in understanding the different emphases of these two tags and learning to flexibly apply their parameters.

Understanding the category tag system of AnQiCMS

AnQiCMS's template system borrows the syntax style of Django, which is concise and powerful. For the call of category information, it provides two main tags:

  1. categoryListTags:As the name implies, this label is mainly used for retrievingCategory List. When you need to display multiple categories on a page, such as sidebar category navigation, homepage category module list, etc., categoryListIt is your first choice. It will return an array of category objects, and you can iterate through it to display the title, link, and other information of each category.categoryListTags have some parameters, such asmoduleId(Specify Model ID),parentIdThe specified parent category ID and others, used for filtering and organizing the list. It is worth noting that if it is not specified when in usecategoryListit is not specifiedcategoryIdThe parameter indeed tries to read the category ID of the current page to filter the content. But if you want to get a list that is unrelated to the current pageof a specific categoryYou can explicitly setparentId="0"to get the top-level category, or throughcategoryIdspecify the category set you want with the parameter. However,categoryListis designed to handlemultiple categoriesthe collection, if you just wantdetails of a specific categoryit is not the most direct or efficient tool.

  2. categoryDetailTags:this is how to obtaindetails of a single specified categoryThe 'tool'. When you know exactly which category to display (for example, the 'About Us' category with ID 5, or the 'Product Case' category with ID 10), and you want to get its detailed attributes such as title, description, link, image, etc., you should usecategoryDetail. It is intended to directly return all available fields of a categorized object, allowing you to accurately extract the required information.

Core solution: make good use of.categoryDetailofidParameter

To implement 'Get the category details of a specified ID instead of the current page', we should focus oncategoryDetailthe tag and specify the target category accurately through itsidparameters.

categoryDetailThe basic usage of tags is as follows:

{% categoryDetail 变量名称 with name="字段名称" id="指定分类ID" %}

There are a few key points:

  • id="指定分类ID"This is the core to achieve the goal. By assigningida specific classification number ID to the parameter (for exampleid="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 the "current page".
  • name="字段名称": Used to specify a specific field of the category you want to retrieve, for example"Title"(Title),"Link"(Link),"Description"(Description),"Logo"(Thumbnail full image) etc. If you need to retrieve multiple fields, you can call it multiple timescategoryDetail, or it is more recommended to set a variable name for the entire category object.
  • 变量名称Optional parameter, if you need to get the entire object of the category for subsequent multiple references to its different fields, you can specify a variable name such asmyCategory)

Example: Get details of category with ID 5

Assuming we want to display the title and link of the category with ID 5 at any location on the page (such as the bottom navigation bar of a website or an independent recommendation module), regardless of which category the current page is.

<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>

In this example, we go throughid="5"The parameter accurately specifies the category to be retrieved as 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 go to the database to search and display the category information with ID 5.

CombinecategoryListby considering the scene

AlthoughcategoryDetailis the way to get details of a single specified ID category, but in some special scenarios, you may first usecategoryListGot a set of categories, then in the loop of this set, use the categories that meet a specific conditioncategoryDetailGet more information (this is usually rare becausecategoryListIt returns itselfitemThe object already contains most of the commonly used fields)

For example, if you want to iterate through all top-level categories, and for one specific category named 'Hot Topic' (assuming its ID is 12), you want to display an additional unique custom field, 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, in most cases,categoryListcyclically returneditemThe object itself contains all the standard fields and custom fields of the classification, directly throughitem.CustomFieldIt will be more efficient, 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 particular category detail,categoryDetailcooperateidthe parameter will be displayedcategoryListin the context

Summary and **practice**

In AnQiCMS, to getcategory details of a specified IDInstead of the category information on the current page, the most direct and efficient way is to usecategoryDetailtag and pass it in accuratelyidparameter to specify the numeric ID of the target category.

  • categoryList: is used to obtainListFor displaying multiple categories scenarios.
  • categoryDetail: is used to obtainDetails of a single category.through.idortoken