How to get and display detailed information of a specified category in AnQiCMS template?

As an experienced CMS website operation personnel in the information security field, I know that it is crucial to display content efficiently and accurately in templates for the attractiveness and SEO performance of the website.Category information is the core of website architecture, which can accurately obtain and present on the front-end page, not only improving user experience but also providing a clear structure for search engine crawling.Today, let's discuss in detail how to obtain and display the detailed information of a specified category in the Anqi CMS template.

Understanding AnQiCMS template and content display basics

The Anqi CMS adopts syntax similar to the Django template engine, allowing us to dynamically retrieve and display website data through simple tags. Template files are usually named with.htmlWith suffix and using double curly braces{{变量}}To output variable values, while logical control and data retrieval are through single curly braces and percentage signs{% 标签 %}

The core to get details of a specified category:categoryDetailTag

In AnQi CMS, to obtain the detailed information of the category, we mainly rely oncategoryDetailThe tag is very flexible, it can automatically identify the classification information of the current page, and can also obtain data of any classification by explicitly specifying parameters.

Generally, when you visit a category list page (such as文章模型/list-分类ID.html),categoryDetailtags can be automatically recognized without any parameters and provide detailed information about the current category.

However, we often encounter the need to display information about a specific category on non-category list pages (such as the homepage, article detail page, or custom page), or to retrieve and display information about other specific categories within a category list. In this case,categoryDetaillabel'sidortokenParameters are particularly crucial.

Specify the category by category ID (id) or URL alias (token) to specify the category

To get the detailed information of the specified category, you cancategoryDetailUsed in tagsidSpecify the numeric ID of the category or usetokenSpecify the URL alias of the category (usually the pinyin or a custom English alias of the category name).

For example, if you want to get the ID of1category title:

<div>分类标题:{% categoryDetail with name="Title" id="1" %}</div>

If you know the URL alias of a category isabout-usand you want to get its description:

<div>分类描述:{% categoryDetail with name="Description" token="about-us" %}</div>

In this way, you can accurately call the required classification information at any location on the website. Moreover, in a multi-site management environment, if you need to call classification information under other sites, you can also usesiteIdSpecify the corresponding site ID with the parameter.

Display detailed information fields of the category.

categoryDetailTags support throughnameSpecify the specific field to be retrieved. The following are some commonly used fields and their calling methods in the template:

  • Category ID (Id):Used to obtain the unique identifier for the category.

    <div>分类ID:{% categoryDetail with name="Id" id="1" %}</div>
    
  • Category title (Title):Get the category name, usually used to display on the page.

    <h1>{% categoryDetail with name="Title" id="1" %}</h1>
    
  • Category link (Link):Get the URL address of the category page. This is very useful for building navigation or 'View More' links.

    <a href="{% categoryDetail with name="Link" id="1" %}">访问{% categoryDetail with name="Title" id="1" %}</a>
    
  • Category description (Description):Get a brief introduction of the category, often used for SEO meta description or the introduction on the category page.

    <p>{% categoryDetail with name="Description" id="1" %}</p>
    
  • Category content (Content):If the category has detailed content in the background (such as description text), it can be obtained through this field. Please note that if the content contains HTML tags, it may be necessary to use|safeA filter to prevent automatic escaping.

    <div class="category-content">{% categoryDetail with name="Content" id="1" render=true %}|safe}</div>
    

    Hererender=trueThe parameter indicates that if the content is in Markdown format, it will be automatically converted to HTML.

  • Parent category ID (ParentId):Get the ID of the parent category of the current category, which is very useful for building multi-level category navigation or breadcrumbs.

    <div>上级分类ID:{% categoryDetail with name="ParentId" id="1" %}</div>
    
  • Category thumbnail (Thumb) and large image (Logo):If the category is set with an image, you can obtain its URL through these two fields.

    <img src="{% categoryDetail with name="Thumb" id="1" %}" alt="{% categoryDetail with name="Title" id="1" %}" />
    
  • Category Banner Group (Images):If the category is configured with multiple Banner images,ImagesThe field will return an array of image URLs, you need to useforloop to traverse and display them.

    {% categoryDetail categoryBanners with name="Images" id="1" %}
    <div class="category-banner-slider">
        {% for imageUrl in categoryBanners %}
            <img src="{{ imageUrl }}" alt="分类横幅图片" />
        {% endfor %}
    </div>
    
  • Number of documents in category (ArchiveCount):Displays the number of documents included in the category, helping users understand the richness of the category content.

    <div>该分类包含文章:{% categoryDetail with name="ArchiveCount" id="1" %}篇</div>
    
  • Custom field:In AnQi CMS backend, you can customize additional fields for the content model. If these custom fields are also applied to categories, you can go throughnameThe parameter is called directly, for example, if you have a custom field namedcategory_icon: custom field

    <i class="icon">{% categoryDetail with name="category_icon" id="1" %}</i>
    

    Or, if you want to loop through all the custom fields, you can useExtraField:

    {% categoryDetail categoryExtraFields with name="Extra" id="1" %}
    {% for field in categoryExtraFields %}
        <div>{{ field.Name }}:{{ field.Value }}</div>
    {% endfor %}
    

Combine other tags to achieve richer display

categoryDetailThe power of the tag lies in its ability to seamlessly integrate with other secure CMS tags, building complex and dynamic page layouts. For example, on a category detail page, you can first obtain the details of the current category, then usearchiveListLabel, and utilizecategoryDetailGet the category ID to filter and display the article list under the category.

{# 假设这是文章详情页,需要获取其分类的信息并显示该分类下的其他文章 #}
{% archiveDetail currentArchive with name="CategoryId" %} {# 获取当前文章的分类ID #}

<div class="category-info">
    <h2>分类名称: {% categoryDetail with name="Title" id=currentArchive %}</h2>
    <p>分类描述: {% categoryDetail with name="Description" id=currentArchive %}</p>
    <a href="{% categoryDetail with name="Link" id=currentArchive %}">进入分类页面</a>
</div>

<div class="related-articles">
    <h3>{{% categoryDetail with name="Title" id=currentArchive %}} 下的其他文章</h3>
    {% archiveList relatedArticles with categoryId=currentArchive type="list" limit="5" %}
        {% for article in relatedArticles %}
            <a href="{{ article.Link }}">{{ article.Title }}</a><br/>
        {% empty %}
            暂无其他文章。
        {% endfor %}
    {% endarchiveList %}
</div>

By this combination, you can easily create content displays with high relevance and dynamism, greatly enhancing the operational efficiency and user experience of the website.

Summary

Of Security CMScategoryDetailTags are powerful tools for retrieving and displaying information about specified categories. Whether it is throughidortokenSpecify precisely or combine other tags to implement complex layouts, it can help you flexibly control the presentation of categorized content in the template.Mastery of this tag's usage will enrich your website content, enhance interactivity, and better serve your users and business objectives.


Frequently Asked Questions

What will the template display if the specified category ID or URL alias does not exist?

IfcategoryDetailLabel specifiedidortokenThe category does not exist, this tag usually will not output any content.This means that the relevant area will remain blank or not displayed on the page.{% if categoryTitle %}Handle this situation elegantly to avoid empty titles or links.

How to get and display a list of all subcategories of a category in the template?

You can usecategoryListLabel collaborationcategoryDetailComplete the tag. First, throughcategoryDetailGet the parent category ID, then use this ID ascategoryListlabel'sparentIdparameter, you can get and loop to display all the subcategories under it.

{# 假设当前在父分类页面,获取其ID #}
{% categoryDetail currentCategoryId with name="Id" %}
<h3>子分类列表</h3>
<ul>
    {% categoryList subCategories with parentId=currentCategoryId %}
        {% for subCategory in subCategories %}
            <li><a href="{{ subCategory.Link }}">{{ subCategory.Title }}</a></li>
        {% endfor %}
    {% empty %}
        <li>该分类暂无子分类。</li>
    {% endcategoryList %}
</ul>