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

Smart usearchiveDetailDirectly get category information with tags

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

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

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

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

In this code,currentArchiveThe variable carries all the data of the current article. Through.CategoryWe accessed a child object containing the article classification information. Then,.Linkthe attribute can be used to obtain the URL link of the category, and.TitleThe display name of the category will be given. 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 get category details

Besides getting directly fromarchiveDetailextract the category object, AnQiCMS also allows us to usecategoryDetailLabel to get detailed information about the classification. This method is suitable for the scenario where you may already have a classification ID and want to independently query all properties of the classification through this ID.

We can first obtain the category ID from the current article, and then pass this ID as a parameter tocategoryDetailtags. The following 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 current article's category ID to a variable namedcategoryId. Then,{% categoryDetail currentCategory with id=categoryId %}Use this ID to retrieve the details of the category, and assign it to a temporary variablecurrentCategoryThe next steps are similar to the first method,currentCategory.LinkandcurrentCategory.TitleShow the category name and link. Although this method adds a step, it demonstrates the flexible collaboration capabilities between AnQiCMS tags.

Application and suggestions in practice

In the 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 features provided by AnQiCMS,breadcrumbLabels, 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 in certain special cases where the classification information may be abnormal, it is advisable to useifLogic judgment to avoid page errors or incomplete display. For example, judge before outputting category links.currentArchive.CategoryorcurrentArchive.Category.Linkwhether it exists.

Through these flexible and powerful template tags, AnQiCMS allows us to display the name and link of the article category 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.


Common Questions (FAQ)

问:AnQiCMS 的文章是否支持多分类?如果支持,如何在模板中显示所有分类?

答:AnQiCMS 默认将每篇文章归属于一个主分类。因此,archiveDetailThe label directly returns the unique category information of the article. If you need to implement a similar "multi-label" effect, AnQiCMS provides an independent label feature, which can be used to add multiple tags (Tag) during article editing, and displayed on the front end.tagListThe label is displayed. The tags associated with the article can serve as auxiliary classification or keywords index for the content.

问:Can I get other information about the category besides the category name and link, such as the category description or image?

答:是的,当然可以。无论是通过 `currentArchive.