How to display the name and link of the current article's category in a frontend template?

Calendar 👁️ 63

In website content operation, the classification information of articles not only helps users quickly understand the content attribution, but is also the key to improving the website navigation experience and search engine optimization (SEO) effect.For friends who use AnQiCMS to build websites, it is a very practical and basic requirement to accurately display the name of the category to which each article belongs and provide a link.AnQiCMS provides powerful and flexible template tags, allowing us to easily implement this feature.

Smart usearchiveDetailTags directly obtain category information

In AnQiCMS template engine, when we are on the article detail page, the system will intelligently identify the article being viewed at the moment.archiveDetailThe tag is the starting point for obtaining all relevant information of the current article. This tag can not only extract the title, content, etc., but also directly access the category information of the article.

To display the category name and link of the current article, we can operate like this:

Firstly, in your article detail template file (usually)archive/detail.htmlor in your custom detail page template, usearchiveDetailLabel to get the complete object of the current article. For easy reference, we can give it a temporary variable name, such ascurrentArchive.

{% archiveDetail currentArchive %}
    <p>所属分类:
        <a href="{{ currentArchive.Category.Link }}">{{ currentArchive.Category.Title }}</a>
    </p>
{% endarchiveDetail %}

In this code block,currentArchiveThe variable holds all the data of the current article. Through.CategoryWe accessed a sub-object containing the article category information. Then, we went through.LinkAttribute can be used to obtain the URL link of the category, and.TitleThe attribute will give the display name of the category. In this way, users can clearly see the category of the article and click the link to jump to the article list page of the category.

Another way: throughcategoryDetailLabel to get category details

Except directly fromarchiveDetailExtract category objects, AnQiCMS also allows us to utilizecategoryDetailTag to get detailed information about the category. This method is suitable when you already have a category ID and want to independently query all properties of the category through this ID.

We can first obtain the category ID of the current article, and then pass this ID as a parameter tocategoryDetailthe tag. Below is the specific implementation method:

{% archiveDetail currentArchive %}
    {# 获取当前文章的分类ID #}
    {% set categoryId = currentArchive.CategoryId %}
    
    {# 使用 categoryDetail 标签,通过ID获取分类详情 #}
    {% categoryDetail currentCategory with id=categoryId %}
        <p>所属分类(另一种方式):
            <a href="{{ currentCategory.Link }}">{{ currentCategory.Title }}</a>
        </p>
    {% endcategoryDetail %}
{% endarchiveDetail %}

Here, we first go through{% set categoryId = currentArchive.CategoryId %}The statement assigns the category ID of the current article to a variable namedcategoryIdThe variable is followed by.{% categoryDetail currentCategory with id=categoryId %}Use this ID to retrieve the details of the category and assign it to a temporary variablecurrentCategory. The next steps are similar to the first method, throughcurrentCategory.LinkandcurrentCategory.TitleShow the category name and link. Although this method adds a step, it shows the flexible collaboration ability between AnQiCMS tags.

Application and suggestions in practice

In actual website design, category names and links are usually placed below the article title, next to the publication date, or as part of the page breadcrumb navigation. Combined with the provided by AnQiCMSbreadcrumbLabel, you can build a more complete navigation path, further optimizing user experience and website structure.

{# 示例:结合面包屑导航 #}
<nav class="breadcrumb">
    {% breadcrumb crumbs %}
        {% for item in crumbs %}
            {% if not forloop.Last %} {# 不是最后一项(当前文章标题)才显示链接 #}
                <a href="{{ item.Link }}">{{ item.Name }}</a> &gt;
            {% else %}
                <span>{{ item.Name }}</span>
            {% endif %}
        {% endfor %}
    {% endbreadcrumb %}
</nav>

To ensure the robustness of the template, especially when migrating content from other systems or when there may be exceptions in the classification information in some special cases, consider usingifLogical judgment to avoid page errors or incomplete display. For example, before outputting category links, first judgecurrentArchive.CategoryorcurrentArchive.Category.Linkwhether it exists.

By using these flexible and powerful template tags, AnQiCMS allows us to display the names and links of the article categories in a very intuitive and efficient way on the front-end template, which is crucial for improving the user experience and SEO performance of the website.


Frequently Asked Questions (FAQ)

Ask: Does AnQiCMS support multi-category articles? If it does, how can all categories be displayed in the template?

Answer: AnQiCMS defaults to assigning each article to a main category. Therefore,archiveDetailThe tag directly returns the unique category information of the article. If you need to implement a similar "multi-tag" effect, AnQiCMS provides an independent tag feature that can be added multiple tags (Tag) during article editing, and displayed on the front end bytagListTags are displayed. The tags associated with the article can serve as auxiliary classification or keyword index for the content.

Ask: Can I get other information about the category, such as the description or image, in addition to the category name and link?

Yes, of course. Whether it is through `currentArchive.

Related articles

Does AnQiCMS website support automatically converting uploaded images to WebP format to speed up display speed?

The loading speed of a website is an important factor for user experience and search engine optimization.Among them, images play an important part in the page content, and their loading efficiency has a significant impact on the overall performance of the website.WebP is a modern image format, compared to traditional JPEG, PNG, it can provide similar or even higher image quality with smaller file size, thereby significantly speeding up web page loading speed.AnQiCMS as a content management system that focuses on efficiency and performance, fully considers the optimization needs of the website in image processing.For "AnQiCMS"

2025-11-07

How to display custom field content of different content models in the frontend template?

The content model feature of AnQiCMS brings great flexibility to website operations, allowing us to define various personalized content structures according to our actual business needs.Whether it is an article, product, event, or any other custom content type, we can add unique fields for it.How can we flexibly display custom fields in the content model on the website front-end template?Let's explore this practical skill together. ### Understanding AnQiCMS's Content Model and Custom Fields Using AnQiCMS

2025-11-07

How to implement batch replacement of specific keywords in article content and display it on the front end in AnQiCMS?

In the daily work of content operation, we often encounter the need to make uniform adjustments to a large amount of article content.This may involve updating the brand name, correcting industry terminology, unify the product model, and even adjusting keyword density for SEO purposes.Manually modifying hundreds or even thousands of articles one by one is undoubtedly a huge task and is prone to errors.AnQiCMS provides a very practical feature that can help us efficiently implement batch replacement of specific keywords in article content and ensure that these changes are displayed correctly on the website front-end.Why do you need to replace keywords in articles in bulk

2025-11-07

How to display the title and link of the previous and next articles on the article detail page?

In AnQi CMS, adding the titles and links of the previous and next articles to the article detail page can not only optimize the user experience on your website, guide them to discover more related content, but also improve the internal link structure of the website to a certain extent.AnQi CMS with its simple and efficient template engine makes the implementation of this feature very intuitive.### Core Feature Revelation: prevArchive and nextArchive Tags The AnQi CMS template system is built-in with two tags specifically designed for handling article navigation: `prevArchive`

2025-11-07

How to display the filing number and copyright information at the bottom of the website in AnQiCMS?

During the operation of a website, whether it is to meet the requirements of laws and regulations or to enhance the professionalism and user trust of the website, clearly displaying the filing number and copyright information at the bottom of the website is an important aspect.AnQiCMS as an efficient content management system provides a very flexible and intuitive way to configure and display these key information. Next, we will introduce step by step how to set and display the filing number and copyright information at the bottom of the AnQiCMS website.### The first step: Fill in the filing number and copyright information in the background First

2025-11-07

How to add watermarks to images on the AnQiCMS website to protect original content?

In today's increasingly rich digital content, protecting the copyright of original works is particularly important, especially for visual content such as images.When your website publishes a large number of carefully crafted images, if they are not protected, it is easy for others to copy and repost them arbitrarily, which not only infringes on your labor results but also weakens the uniqueness of the content.Adding a watermark to an image is a simple and effective means of copyright protection, which can clearly assert ownership of the content without affecting the beauty of the image, effectively deterring unauthorized use.Anqi CMS as a highly efficient

2025-11-07

How to implement keyword search and filtering display on article list or search result pages?

How to allow visitors to quickly find the content they need among the vast amount of information on our website is undoubtedly the key to improving user experience and the effectiveness of content marketing.AnQiCMS (AnQiCMS) knows this and therefore provides a powerful and flexible mechanism to help us easily implement keyword search and content filtering functions on article lists or search result pages.The implementation of this feature mainly revolves around the core template tags of AnQi CMS, allowing us to transform the content of the background management into a user-friendly interactive interface through the exquisite design of the frontend template.###

2025-11-07

How to get and display the Tag list of articles in the front-end template of AnQiCMS?

AnQiCMS as an efficient and flexible content management system, provides a wealth of features to help operators manage and display website content.Among them, the Tag tag function of the article is an important factor for optimizing content organization, improving user experience and SEO effects.Understand how to obtain and display these tags in front-end templates, which is crucial for building a website with comprehensive functions.### In AnQiCMS backend management tags Before delving into the front-end template, let's briefly review how tags work in the AnQiCMS backend

2025-11-07