How to list all related document lists under a specified Tag label?

As an experienced website content expert who deeply understands the operation of security CMS, I know well the readers' thirst for efficient content management and display.Tags (Tags) as an important means of content organization, can greatly enhance the discoverability and user experience of content.In AnQi CMS, properly utilizing the tag function can help your website achieve more refined content aggregation and distribution.

This article will detail how to list all related document lists under the specified Tag label in the Anqi CMS, helping you better organize and present website content.

Deeply Understand the Tag Function of AnQi CMS

The tag feature of AnQi CMS is designed to provide flexible topic classification for content.Tags are more like keywords or thematic words of content, different from strict categorization (Category), allowing a document to be associated with multiple tags, thus achieving cross-references between content across different dimensions.The document tag management function on the back end allows you to create, edit, and manage these tags and apply them to various documents such as articles, products, and more.This mechanism makes the organization of content more flexible, and also provides users with diverse paths for content exploration.

Core Template Tag:tagDataListUsage

To list all related documents under the specified Tag tag on the website front end, AnQi CMS provides a special template tagtagDataListThis tag is a key tool for implementing this feature. It can retrieve and return all associated document lists based on the provided tag ID.

tagDataListBasic syntax and parameters of the tag

tagDataListThe basic usage of the tag is:{% tagDataList archives with tagId="1" %}...{% endtagDataList %}Here,archivesIt is a custom variable name that will hold the document collection retrieved from the label.

This tag supports multiple parameters to meet different content display requirements:

  • tagId: Specify the tag IDThis is the most core parameter, used to specify which label's documents you want to list. For example,tagId="1"It will list all documents under the label with ID 1. It should be noted that if you do not specifytagId的情况下,此标签会自动尝试读取当前Tag页面的TagID,这在构建标签详情页时非常方便。

  • moduleId:按内容模型筛选If you want to list documents under specific content models (such as articles, products), you can use this parameter. For example,moduleId="1"it will only get documents under the article model.

  • order: Sorting rulesYou can set the document sorting method according to your needs, for example,order="id desc"Sorted by the most recent publication,order="views desc"Sorted by views from high to low, or,order="sort desc"Sorted by custom sorting in the background.

  • limit: Control the display quantity.This parameter is used to limit the number of documents returned.limit="10"It will display 10 documents. It also supportsoffsetMode, for examplelimit="2,10"This indicates starting from the second document and fetching 10 data items.

  • type: list type typeThe parameter determines the display behavior of the list. The default value islistat this time,limitThe parameter will accurately control the number of documents returned. If set totype="page", it means you want to enable the pagination feature, which can be combined withpaginationtags to build a complete pagination interface.

  • siteId: Multi-site callUnder the multi-site management mode of AnQi CMS, if you need to call data from other sites, you can do so by specifyingsiteIdthe parameter. Generally, this parameter does not need to be filled in.

tagDataListTags will return aarchivesArray object, you need to traverse and display each document throughfora loop. Eachitemobject (that is, each document in the array) contains rich field information, such as: Id(Document ID),Title(Document Title)、Link(Document Link)、Description(Document description),Logo(cover first image),Thumb[Thumbnail],CreatedTime(Creation Time)、Views(Views)等。You can flexibly select these fields to build the document list according to the template.

实战演示:列出指定Tag下的文档列表

通常情况下,显示指定标签下的文档列表最常用的场景是标签详情页 (tag/list.htmlortag_list.html),或者在某个页面的侧边栏或底部区域展示相关推荐。

示例一:在标签详情页显示分页文档列表

Assuming you are building a tag detail page, you hope to display all the documents under the tag and support pagination.

{# 页面标题可以显示当前标签的名称 #}
<h1>标签:{% tagDetail with name="Title" %}</h1>
<p>描述:{% tagDetail with name="Description" %}</p>

<div class="document-list">
    {# 使用tagDataList标签,type="page"开启分页功能 #}
    {% tagDataList archives with type="page" limit="10" %}
        {% for item in archives %}
        <div class="document-item">
            <a href="{{item.Link}}">
                {% if item.Thumb %}
                <img src="{{item.Thumb}}" alt="{{item.Title}}" class="document-thumbnail">
                {% endif %}
                <h2 class="document-title">{{item.Title}}</h2>
            </a>
            <p class="document-description">{{item.Description}}</p>
            <div class="document-meta">
                {# 获取文档所属分类的标题 #}
                <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                {# 格式化文档发布时间 #}
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>浏览量:{{item.Views}}</span>
            </div>
        </div>
        {% empty %}
        <p>该标签下没有任何文档。</p>
        {% endfor %}
    {% endtagDataList %}

    {# 引入分页导航 #}
    <div class="pagination-container">
        {% pagination pages with show="5" %}
            <ul>
                <li class="page-item {% if pages.FirstPage.IsCurrent %}active{% endif %}">
                    <a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a>
                </li>
                {% if pages.PrevPage %}
                <li class="page-item">
                    <a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a>
                </li>
                {% endif %}
                {% for pageItem in pages.Pages %}
                <li class="page-item {% if pageItem.IsCurrent %}active{% endif %}">
                    <a href="{{pageItem.Link}}">{{pageItem.Name}}</a>
                </li>
                {% endfor %}
                {% if pages.NextPage %}
                <li class="page-item">
                    <a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a>
                </li>
                {% endif %}
                <li class="page-item {% if pages.LastPage.IsCurrent %}active{% endif %}">
                    <a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a>
                </li>
            </ul>
        {% endpagination %}
    </div>
</div>

In this example,tagDataListAutomatically obtain the current tag page.tagId, and display documents in the form of 10 items per page.paginationThe tag is responsible for rendering the complete page navigation through.item.CategoryIdandcategoryDetailThe combination of labels, and we can also display the category name of each document in the document list.

Example two: Display the latest document under the specified label in the sidebar.

If you want to display the latest 5 documents under a specific tag in a fixed area of the website, such as the sidebar, you can do it like this:

<div class="sidebar-block">
    <h3>“Go语言”标签下的最新文档</h3>
    <ul>
        {# 假设标签ID为10,获取最新5篇文档 #}
        {% tagDataList archives with tagId="10" order="id desc" limit="5" %}
            {% for item in archives %}
            <li>
                <a href="{{item.Link}}">{{item.Title}}</a>
                <span>({{stampToDate(item.CreatedTime, "01-02")}})</span>
            </li>
            {% empty %}
            <li>暂无相关文档。</li>
            {% endfor %}
        {% endtagDataList %}
    </ul>
</div>

This example shows how to explicitly specifytagIdandlimitParameters, to achieve accurate content recommendation.

By using flexibilitytagDataListTags and parameters, you can create various forms of tag document lists in the Anqi CMS according to the actual needs of the website, greatly enriching your content display methods and optimizing the user's browsing experience.


Common Questions and Answers (FAQ)

  • tagListandtagDataListWhat are the differences between two tags? tagListTags are mainly used to obtain the "list of tags itself", for example, you may want to display a cloud map of all hot tags at the bottom or sidebars of the website, or list all tag names on the tag homepage.It returns a series of label objects (including label ID, title, link, etc.).tagDataListThe label is used to retrieve the 'document list under the specified label', which is to say, to find all the documents marked with one or more of these labels. In short, tagListFocuses on the tag itself,tagDataListFocuses on the documents associated with the tag.

  • How to display the classification information of each document in the tag document list?ThroughtagDataListLabel loop traverses the document, eachitemobject containsCategoryIdfield, which is the ID of the document's category. You can combinecategoryDetaillabel according to thisCategoryIdTo retrieve and display detailed information about the category, such as the category title or category link. The specific usage is intagDataListit inside the loop{% categoryDetail with name="Title" id=item.CategoryId %}To retrieve the category title.

  • Can I list multiple specified document lists at the same time?According to the current template tag design of AnQi CMS,tagDataListTagstagIdParameters support specifying a single tag ID. If you need to list documents under multiple specific tags at the same time, it is not possible to do so directly through a singletagDataListCall the implementation. The usual solution is, you can use it multiple timestagDataListLabel, call once for each label that needs to be displayed; or, if you have more complex aggregation requirements, you may need to develop a secondary version, perform more complex query logic in the backend controller, and then pass the results to the frontend template for rendering.