Flexible display of category names in AnQiCMS: A comprehensive template invocation guide

Content classification is the core of the website structure, not only helping to organize and manage massive information, but also the key to guiding users to browse the website and enhancing the user experience.Whether it is an article list, product details, or sidebar navigation, clearly displaying category names can make your website content more organized.AnQiCMS relies on its concise and efficient template engine to provide various flexible ways to call and display the names of specified categories, allowing you to easily achieve customized content display according to different scene requirements.

Next, we will start from several common scenarios and discuss in detail how to display category names in the AnQiCMS template.

1. Display the category name of the current page

When you are on a category list page (such as displaying all articles under the "News Information" category), it is often necessary to display the name of the current category at the top of the page. AnQiCMS providescategoryDetailLabels make this operation very intuitive.

If you want to display the title of the current category, just use it like this in the template:

<div>当前分类名称:{% categoryDetail with name="Title" %}</div>

Here are thename="Title"Tell the system the category title you need to get. As no specificidortokenparameter is specified, the system will automatically identify and obtain the category information corresponding to the current page.

Second, display the name of the specified category

Sometimes, you may need to display the name of a specific category on non-categorized pages (such as the homepage, article detail page), for example, the link text of a fixed category in the website footer navigation.At this point, you can accurately obtain it by classifying the ID or URL alias.

To get the name by category ID, assuming your 'About Us' category ID is 5, you can call it like this:

<div>指定分类名称(ID为5):{% categoryDetail with name="Title" id="5" %}</div>

If your category has configured a URL alias (for example, a category named "Industry News", the URL alias isindustry-news), you can also use this alias to get its name:

<div>指定分类名称(别名为industry-news):{% categoryDetail with name="Title" token="industry-news" %}</div>

Through this method, you can flexibly display the specific category name you want to show at any location on the website.

3. Display the category name on the detail page of the content (article, product) details

When a user browses a specific article or product, they usually want to know which category it belongs to, so that they can better understand the content context or jump to the relevant category.AnQiCMS provides the ability to directly obtain category information on the document (article or product) detail page in English.

In the document detail template, you can accessarchiveDetailto get all classification information of the current document, and then extract the classification title:

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

Here, we first assign the current document's category object toarchiveCategorythe variable, and then pass througharchiveCategory.Titleit can conveniently display the category name, andarchiveCategory.Linkit can also be used to build category links, convenient for users to click and jump.

Four, display the category name of each item in the content (article, product) list page

The home page and category list page of the website usually display multiple articles or products, and each item will be marked with its category.In this looping display scenario, AnQiCMS can also allow you to easily achieve it.

You can usearchiveListGet the article list by tag, thenforCombined in the loopcategoryDetailTag, display the category name of each article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <div>
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>
            所属分类:
            {# 通过 item.CategoryId 获取分类ID,再用 categoryDetail 获取分类名称和链接 #}
            {% categoryDetail currentCategory with name="Title" id=item.CategoryId %}
            {% categoryDetail categoryLink with name="Link" id=item.CategoryId %}
            <a href="{{ categoryLink }}">{{ currentCategory }}</a>
        </p>
        <p>{{ item.Description }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In the above example,item.CategoryIdWill provide the current article's category ID in the loop, and then we use this ID tocategoryDetailget the corresponding category name and link from the tags.

V. Display the names of the multi-level category lists

In complex navigation or sidebar scenarios, you may need to display multi-level category lists, including top-level categories and their subcategories.categoryListTags can also handle this.

For example, to display all top-level categories and their first-level subcategory names:

{% categoryList topCategories with moduleId="1" parentId="0" %} {# 获取文章模型的所有顶级分类 #}
    <ul>
        {% for topItem in topCategories %}
        <li>
            <a href="{{ topItem.Link }}">{{ topItem.Title }}</a>
            {% if topItem.HasChildren %} {# 判断当前分类是否有子分类 #}
                <ul>
                    {% categoryList subCategories with parentId=topItem.Id %} {# 获取当前顶级分类的子分类 #}
                        {% for subItem in subCategories %}
                        <li><a href="{{ subItem.Link }}">{{ subItem.Title }}</a></li>
                        {% endfor %}
                    {% endcategoryList %}
                </ul>
            {% endif %}
        </li>
        {% endfor %}
    </ul>
{% endcategoryList %}

Here, we elegantly implement the display of multi-level category names through nesting:categoryListTags andtopItem.HasChildrenJudgment, elegantly implemented the display of multi-level category names.

Summary

AnQiCMS's template system provides a powerful and flexible tag set, allowing you to easily display category names in all corners of the website.These tags can greatly simplify your template development work, whether it is to obtain detailed information of a single category, to dynamically display on a list page, or to build a multi-level navigation structure.Master these calling methods, and you will be able to design and optimize the layout of your website content more freely.


Common Questions (FAQ)

Q1: Why is my category name not displayed?A1: If the category name does not display properly, first please check your template code.categoryDetailorcategoryListThe parameter of the label is correct. EnsureidortokenThe value of the parameter matches the category ID or URL alias set on your backend exactly. Next, checkname="Title"Is it spelled correctly, and has the template file been uploaded and made effective?In some cases, it may be due to cache reasons, try to clear the system cache of AnQiCMS backend, or refresh the browser cache.

Q2: Can I display other information about the category besides the category name? For example, the category description or image?A2: Of course you can.categoryDetailLabels not only support retrievalTitle, but can also retrieve other fields of the category, such asDescription(Classification description),Link(category link),Logo(Classification thumbnail large image),Thumb(Category thumbnail),even the custom fields you define for categories in your backend content model. You just need to changenamethe parameters to the corresponding field names. For example, to display the category description:{% categoryDetail with name="Description" %}.

Q3: How can I get all the subcategory names of a certain category without including the category itself?A3: IncategoryListIn the tags, you can go throughparentIdParameters to specify the retrieval of a subcategory of a category. For example, if you want to get the subcategory with IDXall subcategory names of the category, the code would be:{% categoryList subCategories with parentId="X" %}in a loop.subCategoriesWhen, it will only include its subcategories, and will not include the category with IDXthe category itself.