How to determine if a document belongs to the current page category in the `archiveList` loop?

Calendar 👁️ 68

As an experienced website operations expert, I know that the flexible use of templates in content management systems is the key to enhancing the dynamism of the website and user experience.AnQiCMS (AnQiCMS) provides us with powerful content display capabilities with its efficient architecture based on the Go language and Django-style template syntax.archiveListIn the loop, cleverly judge whether the current document belongs to the category of the current page.

The foundation of flexible content presentation: understandingarchiveListWith category judgment

In daily content operations, we often encounter such needs: on a category page, we may need to display articles under the category, as well as some of the latest and most popular articles in the sidebar or footer, which may come from different categories.If it can be clearly identified which articles are in the current category and which are not, it can greatly improve the user experience and the logic of content organization.

Of Security CMSarchiveListThe tag is a powerful tool for obtaining and displaying document lists.It can flexibly filter out the required documents based on various conditions (such as model ID, category ID, recommendation attributes, sorting method, etc.)When we talk about 'the category of the current page', it usually refers to the category list page that the user is browsing, or the category to which a document detail page belongs.

To implement this judgment, we need two key pieces of information: first is the category ID represented by the current page; second is thearchiveListIn the loop, the classification ID of each document itself. The template tag system of Anqi CMS perfectly supports us in obtaining this information.

Step 1: Get the classification ID of the current page.

No matter where you are on the category list page or on the detail page of a document, as long as the page is associated with a category, Anqi CMS can pass throughcategoryDetailEasily obtain the detailed information of the current category, including its ID.

We can retrieve the ID of the current category and store it in a variable like this:

{# 假设我们当前正处于一个分类页面,或者一个文章详情页,且该文章归属于某个分类。 #}
{# categoryDetail 标签会自动识别当前页面的分类信息。我们从中提取其ID。 #}
{% categoryDetail currentCategory with name="Id" %}
{% set currentCategoryId = currentCategory %}

{# 此时,变量 currentCategoryId 就存储了当前页面的分类ID,比如:10、25等。 #}

Here, we usecategoryDetailThe label obtained the ID of the current category and assigned it tocurrentCategoryIdthis variable. The advantage of doing this is,currentCategoryIdNow is a common variable in the template context, which is convenient for us to compare in subsequent loops.

Second step: Traverse the document list and compare the classification ID.

Next, we usearchiveListTag to get the document list we want to display.To demonstrate the judgment logic, we can deliberately obtain a wider range of document lists, such as the latest 10 articles on the website, which may come from different categories.

{# 假设我们现在需要展示网站上最新的10篇文章,无论它们属于哪个分类。 #}
{# 我们使用 moduleId="1"(通常代表文章模型),不指定 categoryId 参数,以获取一个跨分类的近期文章列表。 #}
{% archiveList allRecentArchives with moduleId="1" type="list" limit="10" %}
    <div class="article-listing">
        {% for archiveItem in allRecentArchives %}
            <div class="article-card">
                <a href="{{ archiveItem.Link }}">
                    <h4>{{ archiveItem.Title }}</h4>
                </a>

                {# 核心判断逻辑:检查当前循环中的文档是否属于当前页面的分类 #}
                {% if archiveItem.CategoryId == currentCategoryId %}
                    <span style="color: green; font-weight: bold;">[当前分类精选]</span>
                {% else %}
                    <span style="color: gray;">[其他推荐]</span>
                {% endif %}

                <p>{{ archiveItem.Description }}</p>
                <small>发布于:{{ stampToDate(archiveItem.CreatedTime, "2006-01-02") }}</small>
            </div>
            {% if not forloop.Last %}<hr>{% endif %} {# 除了最后一项,每项后面加分隔线 #}
        {% empty %}
            <p>很抱歉,当前暂无相关的文章内容。</p>
        {% endfor %}
    </div>
{% endarchiveList %}

In this code block,archiveItemIsarchiveListThe representative of each article in the loop. ByarchiveItem.CategoryIdWe can directly access the category ID of the current loop article. Then, we use{% if archiveItem.CategoryId == currentCategoryId %}This condition judgment, compares the article's category ID with the one we previously obtainedcurrentCategoryId. If they are equal, it means that this article belongs to the category of the current page.

In this way, we can display different styles or information in the same document list, based on the relevance of the article category to the current page, such as adding a special corner mark, changing the title color, or providing more specific prompt information.

Actual application scenarios and value

This judgment ability is not just a simple technical implementation, but also the key to improving the logicality of website content organization and user experience:

  • Enhance user guidance:In a list containing mixed content, clearly identifying the 'current category' helps users quickly find articles most relevant to their interests, reducing the burden of information overload.
  • Personalized content recommendation:You can highlight articles consistent with the current category of the user's browsing path in the related recommendations to enhance the click-through rate and conversion rate of the content.
  • Flexible UI Design:This judgment brings greater freedom to template design.You can design a unique card style, background color, or icon for the current category of articles to make it visually more attractive and easily noticeable at a glance.
  • SEO Optimization Aid:By displaying targeted content, it can further strengthen the theme of the page, helping search engines better understand the content of the page, and potentially improve the ranking of relevant keywords.

The Anqi CMS template system, with its concise and powerful tags, makes these complex logics easy to implement.Mastering this technique of classification judgment in a loop allows you to control the content display more finely, providing website users with a smoother and more personalized browsing experience.


Frequently Asked Questions (FAQ)

1.archiveListlabel'scategoryIdParameters and within loopsitem.CategoryIdWhat is the difference?

archiveListlabel'scategoryIdParameters are used inQuery phaseFilter documents, tell the system 'I only need these specific categories of documents'. For example,categoryId="1"Only documents under the category with ID 1 will be retrieved. Anditem.CategoryIdisarchiveList inside the loopfor eachitem(i.e., each document) has a property that indicates which category the document actually belongs to. This article introduces the technique that you can letarchiveListGet a broader list of documents (for example, without specifyingcategoryIdor specifying multiplecategoryId), then use it inside the loopitem.CategoryIdTo determine the specific attribution of each document, thereby achieving a more refined display logic.

2. In addition to determining whether a document belongs to the current category, I can also useitemWhat conditions can the object perform?

archiveListin the loopitemThe object contains rich information about the document, you can make various conditions judgments:

  • Recommended properties (Flag): {% if item.Flag contains "h" %}(Determine if it is a headline article).
  • Views (Views): {% if item.Views > 1000 %}(Determine if it is a hot article). *

Related articles

How to iterate over and display each filter parameter's optional values and their corresponding links for the `archiveFilters` tag?

Good, as an experienced website operation expert, I am happy to delve into the exquisite features of the `archiveFilters` tag in AnQi CMS and guide you on how to flexibly apply it in practice to build a beautiful and practical content filtering function. --- ## Deep Analysis of AnqiCMS: `archiveFilters` tag, building a dynamic content filtering tool In the world of content management, how to help users quickly find the information they need is the key to improving website experience and conversion rates.Deeply understands this path of AnQi CMS

2025-11-06

How to create a document filtering interface with the `archiveFilters` tag?

AnQi CMS is an efficient and customizable enterprise-level content management system, dedicated to providing powerful and flexible content management tools for website operators.In daily operations, we often need to provide users with a diverse range of content filtering functions to help them quickly find the information they are interested in.At this time, the `archiveFilters` tag provided by AnQi CMS can fully display its capabilities, allowing us to easily build a multi-condition document filtering interface, greatly enhancing the user's content discovery experience.### Understand `archiveFilters`

2025-11-06

How to get the custom model field content of the `archiveDetail` tag?

In the powerful and flexible AnQiCMS content management system, the content model plays a core role, allowing us to build various personalized content structures according to business needs.Whether it is articles, products, activities, or other custom information, we can define unique fields for them.How can you elegantly present these rich custom field contents on the front-end page, which has become the key to template development.

2025-11-06

How does the `archiveDetail` tag in AnQiCMS control the Markdown rendering or lazy loading of document content?

As an experienced website operations expert, I know that in the increasingly fierce online environment, efficient content management and elegant presentation are crucial for the success of a website.AnQiCMS is a powerful and flexible content management system that provides us with great convenience with its template tag system.Today, let's delve deeply into a seemingly simple yet powerful tool: the `archiveDetail` tag of AnQiCMS, especially its mysteries in controlling the Markdown rendering and lazy loading of document content.

2025-11-06

How to get the documents of a specified user, or only display the documents under the current category without including subcategories?

As an experienced website operations expert, I am well aware of the importance of a flexible and efficient content management system for website operations.AnQiCMS, with its lightweight, high-performance, and highly customizable features, has won the favor of many small and medium-sized enterprises and content operators.In the daily content operation, we often need to manage and display the website content in a refined manner, among which, how to accurately obtain the documents of a specific author, or only display the content under the current category while excluding the subcategories, is the key to improving user experience and optimizing the content structure.Today, let's delve deeper

2025-11-06

How does AnQiCMS's document content collection and batch import function improve the efficiency of content construction?

## AnQiCMS: Content collection and batch import, how to drive the leap in content construction efficiency?In today's digital age, website content has become the core of communication between enterprises and users, brand building, and search engine optimization.However, starting from scratch, creating, managing, and updating a massive amount of content is a continuous and time-consuming task for any website operator.The cumbersome collection of materials, manual input, and repeated adjustments not only consume a lot of human and material resources, but may also miss valuable market opportunities.

2025-11-06

How to use AnQiCMS advanced SEO tools (such as Sitemap, Robots.txt) to improve the visibility of a website in search engines?

## Optimize Your Search Engine Footprint: How AnQiCMS Harnesses Sitemap and Robots.txt to Improve Website Visibility In the ever-changing digital world, the search engine visibility of a website is the foundation of its success.No matter if you are running a small and medium-sized enterprise, a self-media platform, or managing multiple sites, ensuring that your content can be discovered and effectively indexed by search engines is crucial for traffic acquisition and brand exposure.

2025-11-06

How does AnQiCMS handle the management of pseudo-static and 301 redirection to solve the problem of URL structure optimization and traffic loss?

## AnQiCMS' Static URL and 301 Redirect: A Smart Solution for URL Optimization and Traffic Protection In the digital wave, a website's URL (Uniform Resource Locator) is far more than a simple address.It is not only the path that users access the page, but also the key clue for search engines to understand the content of the website and evaluate its value.A clear and semantically friendly URL can significantly enhance user experience, improve search engine crawling efficiency and ranking performance.

2025-11-06