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

Content categories are the core of website structure, not only helping to organize and manage massive amounts of information, but also guiding users to browse the website and improving user experience.Whether it is an article list, product details, or side navigation, clearly displaying the category names can make the content of your website more organized.AnQiCMS with its concise and efficient template engine, provides 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 providescategoryDetailLabel, makes this operation very intuitive.

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

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

Herename="Title"Tell the system the category title you need to retrieve. As it is not specifiedidortokenThe parameter, the system will automatically identify and retrieve the corresponding category information on 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-category pages (such as the homepage, article detail page), such as the link text of a fixed category in the website footer navigation.At this time, it can be accurately obtained through the category 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 is configured with 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>

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

3. Display the category name belonging to the content (article, product) details page

When a user browses a specific article or product, they usually want to know which category it belongs to, in order to better understand the content context or navigate to related categories.AnQiCMS provides the ability to directly obtain the category information of the document (article or product) on the detail page.

In the document detail template, you can usearchiveDetailtags to get all the 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, then througharchiveCategory.Titleit can easily display the category name, at the same timearchiveCategory.Linkand 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 homepage and category list page of the website usually display multiple articles or products, and each item will indicate its category below.In this loop display scenario, AnQiCMS can also make it easy for you to implement.

You can usearchiveListTag to get the article list, thenforCombining 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.CategoryIdIt will provide the current article's category ID in the loop, and then we will use this ID tocategoryDetailget the corresponding category name and link in the tag.

V、Display the names of the multi-level category list

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 it.

For example, to display the names of all top-level categories and their first-level subcategories:

{% 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 implement the display of multi-level category names elegantly through nesting:categoryListTags andtopItem.HasChildrenJudgment, elegantly implements the display of multi-level category names.

Summary

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


Frequently Asked Questions (FAQ)

Q1: Why is my category name not displayed?A1: If the category name is not displayed normally, first check your template code.categoryDetailorcategoryListThe label parameter is correct. EnsureidortokenThe value of the parameter matches the category ID or URL alias set in your backend. Next, checkname="Title"Is the spelling correct, and has the template file been uploaded and activated correctly.In some cases, it may be due to cache reasons, try to clear the system cache of AnQiCMS background, 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(Category Description),Link(Category link),Logo(Category thumbnail large image),Thumb(Category thumbnail), even if you customize the fields for categories in your backend content model. You just need to changenamethe parameter to the corresponding field name. For example, to display the category description:{% categoryDetail with name="Description" %}.

Q3: How can I get all the subcategory names of a certain category but not include the category itself?A3: IncategoryListYou can filter through the tags.parentIdParameters to specify the retrieval of subcategories of a certain category. For example, if you want to get the subcategories of 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.