How to display the related article list of the `tagDataList` tag in AnQiCMS according to the specified Tag ID?

Calendar 👁️ 71

Manage and display content in Anqi CMS, especially when it is necessary to aggregate articles based on specific themes or keywords, the Tag feature is particularly important.It can not only help visitors quickly find the content they are interested in, but also effectively improve the internal link structure and SEO performance of the website.Today, let's delve into a very practical template tag in AnQiCMS:tagDataList,See how it accurately displays a list of related articles based on the specified Tag ID.

Core function analysis:tagDataListBasic usage of tags

AnQiCMS provides rich and flexible template tags,tagDataListThis is one of them, specifically used for extracting a list of articles (or documents) associated with a specific tag from a database. Imagine you have a tag about "search engine optimization" and you want to display all articles with this tag on a page.tagDataListThis is the key to achieving this goal.

Its basic usage is very intuitive:

{% tagDataList archives with tagId="1" %}
    {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description|truncatechars:100 }}</p>
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
        </div>
    {% empty %}
        <p>当前标签下暂无文章。</p>
    {% endfor %}
{% endtagDataList %}

In this code block:

  • tagDataList archivesWe define a variable namedarchivesThe variable, all the article data that meets the conditions will be stored in this variable. You can name this variable according to your habit.
  • with tagId="1"This istagDataListThe most critical parameter, which explicitly specifies the Tag ID under which we want to retrieve the articles. You need to replace"1"with the actual Tag ID you want to call.
  • {% for item in archives %}: Due toarchivesis a list of articles (array objects), we need to iterate over and display each article throughfora loop. Inside the loop,itemthe variable represents all the fields of the current article, for exampleitem.Title(Article Title),item.Link(article link),item.Description(Article description) and so on.
  • {% empty %}: This is a very humanized design. IfarchivesThere are no articles in the list,emptythe content within the block will be displayed to avoid the page from appearing blank or crashing.
  • {% endtagDataList %}: withtagDataListPair occurrence indicates the end of the label scope.

How to get Tag ID?You can view the detailed information of each tag on the "Content Management" -> "Document Tag" page in the AnQiCMS backend, which usually includes its unique ID. In addition, the template on the tag detail page (usuallytag/detail.html),tagDataListIf the label is omittedtagIdThe parameter will intelligently automatically read the Tag ID of the current page, thereby displaying a list of articles associated with the current tag page.

Flexible control of content:tagDataListthe critical parameters

excepttagId,tagDataListIt also provides a series of parameters to help us finely control the acquisition and display of articles:

  1. moduleId: Specify the content model.AnQiCMS supports flexible content models, such as the "Article" model (the default ID may be 1) and the "Product" model (the default ID may be 2). If you only want to display articles under a specific content model, you can usemoduleIdParameter. For example,with tagId="1" moduleId="1"It will only retrieve the articles under the "Article" model with Tag ID 1.

  2. limitControl the number of displayed items.This parameter is used to control the number of articles displayed. If you only want to display the latest 5 articles, you can setlimit="5". It also supports offset, such aslimit="2,10"Will start from the second article and display the next 10 articles.

  3. order: Sorting methodThe display order of the articles can be controlled byorderparameters. Common sorting methods include:

    • order="id desc": Sort by ID in descending order (newest release).
    • order="views desc":Sort by views in reverse order (most popular).
    • order="sort desc": Sort by custom sorting in the background in descending order.
  4. typewithpagination: List type and pagination.

    • type="list"(Default): Use when you only want to display a fixed number of articles without pagination.
    • type="page"When you want to display a list of articles and support pagination, you need to combinepaginationTags to build page navigation
  5. siteId: Multi-site compatibilityIf you are using the multi-site management feature of AnQiCMS and need to call data from other sites, you cansiteIdSpecify the parameter. It is usually not necessary to set this parameter for single-site users.

Case study: Build a list of Tag-related articles.

Let us go through a more complete example, to see how to apply it in an actual templatetagDataList.

Case one: Display the list of popular tags in a non-tab page

Assume we want to display the latest 3 articles under the "SEO Optimization" tag in a sidebar on the website homepage.

{# 假设 Tag ID 为 10 代表“SEO优化”标签 #}
<h2>热门SEO文章</h2>
{% tagDataList archives with tagId="10" moduleId="1" order="id desc" limit="3" %}
    <ul>
    {% for item in archives %}
        <li>
            <a href="{{ item.Link }}" title="{{ item.Title }}">
                {{ item.Title|truncatechars:25 }}
            </a>
            <p class="post-date">{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
        </li>
    {% empty %}
        <li>暂无相关文章。</li>
    {% endfor %}
</ul>
{% endtagDataList %}

Here, we usemoduleId="1"Make sure to only get the articles,order="id desc"Ensure they are the most recent,limit="3"which limits the number of displayed items.

Case two: Displaying paginated article lists on the Tag detail page

When a user clicks on a tag to enter its detail page (for exampletag/list.htmlortag/index.htmlWhen this tag is selected, it usually needs to display all articles under the tag and provide pagination functionality.

{# 假设当前页面是 Tag 详情页,tagId 会自动获取 #}
<h1>标签:{% tagDetail with name="Title" %} 相关文章</h1>
<div class="tag-description">
    <p>{% tagDetail with name="Description" %}</p>
</div>

<div class="article-list">
{% tagDataList archives with type="page" limit="10" %} {# 每页显示10篇文章 #}
    {% for item in archives %}
        <div class="article-card">
            <a href="{{ item.Link }}">
                {% if item.Thumb %}
                    <img src="{{ item.Thumb }}" alt="{{ item.Title }}" class="article-thumb">
                {% else %}
                    {# 可以放置一个默认缩略图 #}
                    <img src="/static/images/default-thumb.png" alt="默认图片" class="article-thumb">
                {% endif %}
                <h3>{{ item.Title }}</h3>
            </a>
            <p>{{ item.Description|truncatechars:120 }}</p>
            <div class="article-meta">
                <span>分类:<a href="{% categoryDetail with name='Link' id=item.CategoryId %}">{% categoryDetail with name='Title' id=item.CategoryId %}</a></span>
                <span>发布:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                <span>阅读:{{ item.Views }}</span>
            </div>
        </div>
    {% empty %}
        <p>当前标签下还没有任何文章。</p>
    {% endfor %}
{% endtagDataList %}
</div>

{# 分页导航 #}
<div class="pagination-nav">
    {% pagination pages with show="5" %} {# 最多显示5个页码按钮 #}
        {% if pages.PrevPage %}
            <a href="{{ pages.PrevPage.Link }}" class="prev">上一页</a>
        {% endif %}
        {% for pageItem in pages.Pages %}
            <a href="{{ pageItem.Link }}" class="page-number {% if pageItem.IsCurrent %}active{% endif %}">{{ pageItem.Name }}</a>
        {% endfor %}
        {% if pages.NextPage %}
            <a href="{{ pages.NextPage.Link }}" class="next">下一页</a>
        {% endif %}
    {% endpagination %}
</div>

In this case,tagDataList archives with type="page" limit="10"will fetch the current

Related articles

How to round numbers or retain a specified number of decimal places in AnQiCMS templates?

In the presentation of website content, the accuracy and readability of numbers are crucial for improving user experience and the effectiveness of information communication.Whether it is to display product prices, financial data, or various statistical indicators, we often need to round numbers or retain specific decimal places.As a template developer or content operator for AnQiCMS, understanding how to flexibly control the display format of numbers is a key step in achieving professional content presentation.AnQiCMS's template system borrowed the syntax of Django template engine

2025-11-09

Does AnQiCMS support displaying collected articles directly on the website?

## AnQiCMS content collection feature: let the article go online directly, or go further?In the era of information explosion, continuously delivering high-quality content to websites is a major challenge faced by many operators.The efficiency of content production is directly related to the activity of the website, user stickiness, and even search engine rankings.

2025-11-09

How to convert the case of a string and display it in AnQiCMS template?

In website content operation, we often encounter the need to format text.In order to unify the brand image, improve the readability of the page, or optimize the search engine friendliness, converting strings to uppercase and lowercase is a fundamental and practical skill.AnQiCMS as an enterprise-level content management system developed based on the Go language, with its flexible template engine syntax (similar to Django templates), provides us with a rich set of tools to accurately control the display of content.Today, let's delve deeply into how to use AnQiCMS

2025-11-09

How does the AnQiCMS website achieve fast content display under high concurrency through static caching and Go language features?

In today's rapidly changing digital world, the speed of website access and stability under high concurrency have become key factors in user experience and search engine rankings.For a content-rich corporate website or self-media platform, ensuring that content is presented quickly while efficiently utilizing server resources is a major challenge for operators.AnQiCMS (AnQiCMS) with its powerful core based on the Go language and the exquisite static caching mechanism, provides an excellent solution to this problem.### Go Language Empowerment

2025-11-09

How to add captcha functionality to the AnQiCMS comment form to reduce the display of spam comments?

In website operation, spam comments are undoubtedly a headache.They not only affect the user experience of the website, dilute high-quality discussion content, but may also harm the website's SEO performance in an invisible way, even occupying valuable server resources.Faced with increasingly intelligent spam comment robots, we need more effective defense measures.Fortunately, AnQiCMS provides a simple and powerful solution - adding a captcha feature to the comment form, which can significantly reduce the intrusion of spam comments.

2025-11-09

How to automatically replace and display anchor text in article content in AnQiCMS?

In website operation, anchor text is an important element that connects different pages, enhances content relevance, and optimizes search engine rankings.A well-crafted anchor text can not only guide users to discover more relevant information but also help search engines better understand the content and structure of the page and website.For those of you who use AnQiCMS (AnQiCMS) to manage content, the system provides an efficient and intelligent mechanism to automatically convert keywords in the article content into linked anchor text, thereby greatly improving the efficiency and effectiveness of content operation.

2025-11-09

How to optimize the page layout display using AnQiCMS template inheritance (`extends`) and inclusion (`include`) tags?

In website construction, how to organize the page structure efficiently to make the code reusable and easy to maintain is the key to improving development efficiency and project quality.AnQiCMS has fully considered this point, providing powerful template inheritance (`extends`) and template inclusion (`include`) tags, allowing us to build websites like building blocks, easily realizing complex page layout and display optimization.

2025-11-09

How to judge whether a string contains a certain keyword in AnQiCMS template and display the result?

In website operation, we often encounter such needs: dynamically adjusting the display mode of content based on specific information of web page content.For example, if an article title contains keywords such as 'Promotion' or 'Activity', we may want it to display an eye-catching label automatically; or, in order to review content and improve user experience, we need to check if there are certain sensitive words in the article and take appropriate actions accordingly.

2025-11-09