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> >
{% 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.