How to retrieve the associated document list according to TagID in AnQiCMS?

Unlock content association: How to efficiently retrieve related document lists based on TagID in AnQiCMS

In a content management system, tags (Tag) are an important tool for connecting content, improving user experience, and optimizing search engine visibility.AnQiCMS, as an enterprise-level content management system developed based on the Go language, is well-versed in this field and provides flexible and powerful tag functions.For website operators, how to accurately obtain and display a list of associated documents based on a specific tag ID is a key link in the realization of refined content operation.Today, let's delve deeply into this practical feature of AnQiCMS, allowing you to easily navigate the associations between content, providing users with a richer exploration path.

The unique value of (Tag) in AnQiCMS

In AnQiCMS's design concept, tags are not just a simple classification of content, but more like bridges that organically link documents under different categories and even different content models (such as articles, products).Tags differ from categories in terms of hierarchy and exclusivity, they have high flexibility and aggregation.A document can belong to a category but can also be tagged with multiple tags, thus being discovered from multiple dimensions.For example, an article about "AnQiCMS Deployment Tutorial" may be tagged with "AnQiCMS", "Deployment", "Go Language", "Server Operations", and many other tags, greatly expanding the reach of the content.

The AnQiCMS tag management feature allows you to easily view, add, edit, and delete document tags. Each tag has fields such as name, index letter, custom URL, and description, and you can even set independent SEO titles and keywords for tags. This is crucial for building clear website themes and improving the ranking of specific keywords.

On the template level, AnQiCMS provides special tags for labels, such astagListUsed to get the tag list of all or specified documents on the website,tagDetailUsed to obtain detailed information about a specific tag. Today, we will focus ontagDataListthe tag, which is the core tool for obtaining the list of associated documents according to TagID.

Core: utilizingtagDataListLabel to get document list

When we need to retrieve all documents associated with a known tag ID,tagDataListTags are our first choice. They can accurately filter out all documents tagged with the provided TagID from the content library.

Let's understand its usage through a specific scenario. Assume you have a TagID of123The tag, possibly named "Go Language Development", now you want to display all articles related to "Go Language Development" on a page.

The basic calling method is very intuitive:

{% tagDataList archives with tagId="123" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <p>{{item.Description}}</p>
            <div>
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>浏览量:{{item.Views}}</span>
            </div>
            {% if item.Thumb %}
            <img src="{{item.Thumb}}" alt="{{item.Title}}">
            {% endif %}
        </a>
    </li>
    {% empty %}
    <li>
        该标签下暂无相关文档。
    </li>
    {% endfor %}
{% endtagDataList %}

In this code block,tagId="123"Tell AnQiCMS explicitly that we want to get all documents associated with the tag ID 123.archivesIt is a custom variable name that will carry the document list obtained. Then, we iterate over this list and can access{% for item in archives %}the document fields to build the display content.item.Title/item.Link/item.Description/item.CreatedTime/item.Views/item.Thumbetc.{% empty %}A block is a very considerate design, whenarchivesWhen the list is empty, it elegantly displays a prompt message to avoid blank pages or errors.

It is worth mentioning that if you are currently on a tag detail page (such astag/list.htmlortag/detail.htmlSuch a template file), AnQiCMS will automatically identify the TagID of the current page. In this case, you can even omit it.tagIdParameters to make the code more concise:

{% tagDataList archives %} {# 在标签详情页,会自动读取当前页面的 TagID #}
    {% for item in archives %}
    <li>
        {# 展示文档信息 #}
        <a href="{{item.Link}}">{{item.Title}}</a>
        <p>{{item.Description}}</p>
    </li>
    {% empty %}
    <li>
        当前标签下暂无相关文档。
    </li>
    {% endfor %}
{% endtagDataList %}

Advanced usage: combine other parameters for filtering and sorting

tagDataListThe power of tags is not limited to retrieving documents based on TagID, it also supports a series of parameters to help you finely control the retrieval and display of documents:

  • moduleId: Content model restrictionIf you only want to retrieve associated documents under a specific content model (such as "article model" or "product model"), you can usemoduleIdthe parameters. For example,moduleId="1"usually represents an article model. “`twig {% tagDataList articles with tagId=“123” moduleId=“1” %} {# Only display the document of the article model #} {% endtagDataList