How to get and display the name and link of the current document's category?

In the content detail page of the website, we often hope to clearly display the classification information of the current document, such as the name of the classification and the link to that classification.This not only helps visitors better understand the content structure, but also enhances the website's navigation and user experience.The Anqi CMS provides us with various flexible ways to meet this requirement.

Next, we will discuss several methods for obtaining and displaying the name and link of the current document's category in AnQiCMS templates. You can choose the most suitable solution according to your actual template design and requirements.

Method one: Directly access the classification attribute of the document object

In the document detail page of AnQi CMS, the system will automatically provide us with the complete information of the current document, which is usually carried by a global variable namedarchive. ThisarchiveThe object is very powerful, including detailed data of its category. We can directly accessarchiveObjects'Categoryattributes to get the name and link of the category.

It should be noted that,archive.Categoryitself is an object, it hasTitle(Category Name) andLinketc. In the template, we can call them like this:

{% if archive.Category %}
    <p>当前文档所属分类:<a href="{{ archive.Category.Link }}">{{ archive.Category.Title }}</a></p>
{% else %}
    <p>该文档未找到所属分类。</p>
{% endif %}

In the above code snippet, we first use{% if archive.Category %}To determine if the current document is successfully associated with a category. If it exists, display a paragraph containing the category name and link.{{ archive.Category.Link }}will output the URL of the category page,{{ archive.Category.Title }}This method will display the category name. This method is the most direct and concise, and is suitable for most scenarios where only basic information of the category needs to be displayed.

Method two: Obtain more detailed category information through category ID

Sometimes, we may need to retrieve category information more flexibly, or we may want to obtain more customized fields of the category (such as the category's Logo, description, etc.). In this case, we can first obtain the category ID from the current document object, and then use the ones provided by the Anqi CMS.categoryDetailLabel to query the details of this category.

Firstly, we can use.{{ archive.CategoryId }}to get the ID of the category the current document belongs to. Then, pass this ID as a parameter tocategoryDetailTags:

{% if archive.CategoryId %}
    {% categoryDetail currentCategory with id=archive.CategoryId %}
        <p>当前文档所属分类:<a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a></p>
        {# 如果需要,还可以显示更多分类信息,例如: #}
        {# <img src="{{ currentCategory.Logo }}" alt="{{ currentCategory.Title }}" /> #}
        {# <p>{{ currentCategory.Description }}</p> #}
    {% endcategoryDetail %}
{% else %}
    <p>该文档未找到所属分类。</p>
{% endif %}

Here, we declare a variable namedcurrentCategoryto receivecategoryDetailthe category details object returned by the tag. Byid=archive.CategoryIdthe parameter, we tell the system to find the object with ID ofarchive.CategoryId[en]The category. After that, we can access it like thisarchive.Category[en]Similarly, through this{{ currentCategory.Link }}and{{ currentCategory.Title }}[en]To obtain the category link and name. This method is very useful when it is necessary to have more fine-grained control over category information.

Method three: Use breadcrumb navigation to uniformly display category paths

In addition to displaying the category name and link individually, the common Breadcrumb feature in website navigation also presents the category hierarchy of the document and its level in the entire website structure very well. The Anqi CMS providesbreadcrumbTags can conveniently generate a complete navigation path, naturally including the category the current document belongs to.

UsebreadcrumbWhen labeling, it returns an array containing all hierarchical information on the navigation path, each element having a name and a link. We usually traverse this array to build the breadcrumb navigation:

{% breadcrumb crumbs %}
    <nav class="breadcrumb">
        {% for item in crumbs %}
            {% if not forloop.Last %} {# 如果不是最后一个元素,则显示链接和分隔符 #}
                <a href="{{ item.Link }}">{{ item.Name }}</a> &gt;
            {% else %} {# 最后一个元素通常是当前页面,只显示名称,不带链接 #}
                <span>{{ item.Name }}</span>
            {% endif %}
        {% endfor %}
    </nav>
{% endbreadcrumb %}

In this example,crumbsThe variable will include all intermediate categories from the homepage to the current document. Eachitemall have,Name(Display Name) andLink(Link Address) properties. Throughforloop.LastThis auxiliary variable can easily distinguish the current document (usually the last element of the breadcrumbs) and apply different styles, such as not adding a link.Breadcrumbs navigation not only shows the current category, but also provides the complete context, which is very user-friendly.


In summary, AnQi CMS provides various flexible and easy-to-use methods to obtain and display the names and links of the document categories.You can choose the most suitable method for your website based on the specific template design, the amount of information to be displayed, and the overall consideration of the navigation path.Whether it is a concise direct call, a flexible ID query, or a comprehensive breadcrumb navigation, Anqi CMS can help you easily optimize the presentation of content.

Common Questions and Answers (FAQ)

1. Why is itarchive.Categoryorarchive.CategoryIdnot possible to get the category information sometimes?This usually happens when the document is not correctly associated with any category, or the category to which the document belongs has been deleted or disabled by the administrator. It is used in templates.{% if archive.Category %}or{% if archive.CategoryId %}Judging is a good habit, which can avoid errors or blank displays on the page when there is no classification information.

2. I want to display a small category icon after the category name. How should I do that?You can consider using the second method, which is tocategoryDetailTo get category information, use tags. When creating categories in the background, Safe CMS supports uploading thumbnails for categories (Thumb) or Logos (Logo). You can call them in the template like this:

{% categoryDetail currentCategory with id=archive.CategoryId %}
    <p>当前分类:<a href="{{ currentCategory.Link }}">
        {% if currentCategory.Thumb %}<img src="{{ currentCategory.Thumb }}" alt="{{ currentCategory.Title }}" style="height: 20px; vertical-align: middle;">{% endif %}
        {{ currentCategory.Title }}
    </a></p>
{% endcategoryDetail %}

If your classification model has customized other fields to store icon information, it can also be done throughcurrentCategory.您的自定义字段名to get.

3.archive.CategoryandcategoryDetailWhat are the differences in the classification information obtained? Which one should I choose?

  • archive.Categoryis an intrinsic property of the document object, which directly provides the basic information about the document's category (such asTitle,LinkIts advantages are the most concise usage and small performance overhead, as the information is loaded with the document.
  • categoryDetailis an independent template tag that allows you to query by category ID (or other identifier)anyThe detailed information of the category. Its advantages are that it is more powerful and can obtain more properties of the category (such asDescription,Logo,Images[and fields defined in the classification model), even querying categories not belonging to the current document can be done.

Suggestion for selection:If you only need to display the basic information such as the name and link of the current document category,archive.Categoryis the most recommended and the most efficient choice. If you need to access more detailed information about a category (such as category description, Banner image, custom fields, etc.), or need to dynamically retrieve data for different categories based on a category ID, thencategoryDetailThe label will be a more suitable choice.