How to get and display the name and link of the category to which the current document belongs?

Calendar 👁️ 60

On 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 the classification.This not only helps visitors better understand the content structure, but also improves the website's navigation and user experience.AnQi CMS provides us with a variety of 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 automatically provides 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 accessarchivethe object'sCategoryproperties to get the category name and link.

This is something that needs to be noted,archive.CategoryItself is an object, it hasTitleCategory name andLinkProperties such as category links. We can call them in the template:

{% 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 successfully associated with a category. If it exists, display a paragraph containing the category name and link.{{ archive.Category.Link }}It will output the URL of the category page, and{{ archive.Category.Title }}This will display the category name. This method is the most direct and concise, and is suitable for most scenarios where only basic information about the category needs to be displayed.

Method two: Retrieve more detailed category information through the category ID

Sometimes, we may need to obtain classification information more flexibly, or we may want to obtain more custom fields of the classification (such as the classification Logo, description, etc.). In this case, we can first obtain the classification ID from the current document object, and then use the Anqi CMS providedcategoryDetailTag to query the detailed information of the category.

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

{% 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 declared a variable namedcurrentCategoryThe variable to receivecategoryDetailThe category detail object returned by the tag. Throughid=archive.CategoryIdThe parameter, we tell the system to look up ID forarchive.CategoryIdAfter that, we can access it likearchive.Categoryby the way, through{{ currentCategory.Link }}and{{ currentCategory.Title }}Get the link and name of the category. 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 the category path.

In addition to displaying the category name and link separately, the common breadcrumb feature in the website navigation can also effectively display the category of the document and its hierarchical relationship in the entire website structure. Anqi CMS providesbreadcrumbTags can conveniently generate a complete navigation path, which naturally includes the category of the current document.

UsebreadcrumbWhen a tag is clicked, it returns an array containing all the hierarchical information on the navigation path, each element having a name and a link. We usually iterate over 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. EachitemhasName(display name) andLink(link address) attribute. Byforloop.LastThis auxiliary variable, we can easily distinguish the current document (usually the last element of the breadcrumb), and give different style processing, such as not adding links.Breadcrumb navigation not only shows the current category but also provides the complete context, which is very user-friendly.


In summary, Anqi CMS provides a variety of flexible and easy-to-use methods for retrieving and displaying the name and link of the document category.You can choose the most suitable method for your website based on specific template design, the amount of information to be displayed, and the overall consideration of the navigation path.Whether it is a simple direct call, a flexible ID query, or a comprehensive breadcrumb navigation, Anqi CMS can help you easily optimize the presentation of content.

Frequently Asked Questions (FAQ)

1. Why is it sometimesarchive.CategoryOrarchive.CategoryIdunable to obtain category information?This usually happens when the document is not correctly associated with any category, or the category of the document has been deleted or disabled by the administrator. It is used in the template.{% if archive.Category %}or{% if archive.CategoryId %}It is a good habit to make judgments, which can avoid page errors or blank displays when there is no classification information.

2. How do I display a small icon after the category name?You can consider using the second method, that is, bycategoryDetailTag to get category information. When creating categories in the background, AnQi CMS supports uploading category thumbnails (Thumb) or Logo (Logo). You can call it 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, you can also do it throughcurrentCategory.您的自定义字段名Get it.

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

  • archive.CategoryIt is an inherent property of the current document object, which directly provides the basic information about the category of the document (such asTitle,LinkIts advantages are that it uses the simplest structure, with minimal performance overhead, because the information is loaded with the document.
  • categoryDetailIt is an independent template tag that allows you to query by category ID (or other identifier)anyThe details of the category. Its advantages are more powerful functions, such as being able to obtain more attributes of the category (such asDescription,Logo,ImagesAnd even categories defined in the classification model can be queried, even if they do not belong to the current document category.

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

Related articles

How to implement lazy loading of images in document content to optimize display performance?

## Optimize Website Display Performance: Practical Guide to Implementing Lazy Loading of Images in AnQi CMS In today's internet era, the loading speed of a website is crucial for user experience and search engine rankings.Large image files are often one of the main culprits slowing down website loading speeds. 幸运的是,image lazy loading (Lazy Loading) technology can effectively solve this problem.

2025-11-08

How to call and display the cover image, thumbnail, or group image of a specified document?

It is crucial to have visually appealing content when building and operating a website.AnQiCMS provides a flexible way to manage and display the images of documents (including articles, products, etc.), whether as an eye-catching cover, a clever thumbnail, or a colorful group photo.Understand how to correctly call these images, which can help you better design and optimize the user experience of the website.AnQiCMS provides several main image fields for different content types, which have clear uses in the template: * **Cover logo (Logo)**

2025-11-08

How to display the document title, publish time, and view count on the document detail page?

Manage website content in AnQi CMS and ensure its accurate and beautiful display on the front-end page, which is the core of daily operation work.For each document detail page, clearly presenting the title, publication time, and views of the document can not only effectively improve user experience, but is also an important link in the transmission of content information.The AnQi CMS provides powerful and flexible template tags, allowing us to easily meet these requirements. ### Locate the document detail page template To modify the display content of the document detail page, we first need to find the corresponding template file.In AnQi CMS template structure

2025-11-08

How to display mathematical formulas and flowcharts on AnQiCMS web pages?

Today, with the increasingly rich digital content, it often seems inadequate to convey complex information solely through text.Especially in fields such as science, technology, and education, mathematical formulas and flowcharts are the key to expressing rigorous logic and clear steps. 幸运的是,AnQiCMS provides a simple and powerful way to easily integrate and display these professional contents on your web page. This article will introduce in detail how to use the Markdown editor and third-party libraries on your AnQiCMS website to achieve perfect presentation of mathematical formulas and flowcharts.

2025-11-08

How to display a list of recommended documents related to the current document on the document detail page?

In website content operations, providing users with relevant recommended content not only effectively enhances the depth and duration of page browsing, but also optimizes user experience, indirectly assisting in the SEO performance.In AnQiCMS (AnQi Content Management System), implementing this feature is flexible and direct.Next, we will discuss how to seamlessly integrate and display the recommended document list closely related to the current document on the document detail page. ### Core Value: Why should recommended documents be displayed on the document detail page?

2025-11-08

How to display the titles and links of the previous and next documents on the document detail page?

In website content operation, it is crucial to provide readers with a smooth reading experience.If a user finishes reading a document and can easily navigate to the previous or next related content, it not only effectively extends the user's stay on the website and increases page views, but also helps search engines better understand the structure and content relevance of the website, thereby having a positive impact on SEO.AnQiCMS (AnQiCMS) has fully considered this point in the design of template functions, providing simple and powerful tags, making it easy to display the title and link of the previous and next documents on the document detail page

2025-11-08

How to call and display the data of the custom model field in the document?

In Anqi CMS, the flexible content model is one of its core strengths, allowing us to create various information carriers such as articles, products, and more according to different business needs and define unique properties for them.How to present the data of these customized fields in the website front-end after we have set them for the document is a major concern for many users.This article will provide a detailed explanation of this process, helping you easily master the custom model fields of Anqi CMS. ### Understanding the Value of Custom Model Fields Firstly, let's clarify the importance of custom model fields.

2025-11-08

How to filter and display a document list based on specific recommendation attributes (such as 'Headline', 'Recommendation')?

The flexible application of content recommendation properties in AnQiCMS is a key link in enhancing the visibility and user experience of website content.Whether you want to highlight important news on the homepage or recommend selected content in specific sections, AnQiCMS provides a convenient and efficient solution.This article will delve into how to filter and display document lists based on specific attributes such as 'Headlines' and 'Recommendations', helping you better manage and present website content.### Part One: Understanding Recommendation Properties and Their Settings Recommendation properties are a very practical feature in the AnQiCMS content management system

2025-11-08