AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template tag system is the key to achieving personalized display for content operators and website developers. Today, we will delve into one very practical tag:{% tagList %}It is especially important to skillfully use it to accurately obtain and display the associated tags based on the document ID. This is crucial for building a website with rich content and strong relevance.

Label and content association: Improve user experience and SEO

In content operations, it is a common practice to add 'Tags' to articles, products, and other documents.It can not only help users quickly find relevant content, but also provide clearer semantic information for search engines, thereby optimizing the internal link structure and SEO performance of the website.Aqie CMS is well-versed in this, providing a comprehensive tag management function, which is reflected in its powerful template tags.

Core function analysis:{% tagList %}withitemIdParameter

{% tagList %}The tag is the core tool of Anqi CMS used to list document tags.Its basic usage is to traverse the tags of a website. However, to achieve the specific requirement of 'retrieving associated tags by document ID', we must rely ontagLista key parameter of the tag:itemId.

UnderstandingitemIdpower

itemIdParameters are like an intelligent filter, it tells{% tagList %}Tags only return those tags associated with the specified document ID. Its usage is very flexible:

  1. Default behavior (current document): When you use directly in a document detail page (such as an article detail page){% tagList tags %}When labeling, if not explicitly specifieditemIdThe system will intelligently identify the document ID of the current page and automatically retrieve the associated tags. This is the most common and convenient usage.

  2. Explicitly specify the document IDIf you want to get the associated tags of any document on the website rather than the document of the current page, you can specify the document ID in the following form. For example,itemId="[文档ID]".itemId="10"Will retrieve all tags associated with the document with ID 10.

  3. Retrieve all tags (not based on document association): Although it is a bit off topic for today's theme, but understanditemId="0"The function is also very helpful. WhenitemIdis set to"0"then,tagListWill no longer be filtered according to any specific document, but will return all tags on the website. This is usually used to build a tag cloud or tag index page for the entire site.

The data structure is returned

When{% tagList %}After the tag is executed, it will store the list of matched tags in a variable (for example, we often name ittagsThis variable is an array object, where each element represents an independent tag and contains the following key attributes for display in the template:

  • Id: Unique ID of the label.
  • Title: The display name of the tag, such as “website operation”, “SEO optimization”.
  • Link: The URL link of the tag, clicking on it will usually jump to the document list page under the tag.
  • Description: Description information of the tag (if set in the background).
  • FirstLetter: The initial letter of the tag name (used for sorting by letters, etc.).
  • CategoryId: If the tag is associated with a specific category (although the document tags themselves are not categorized, certain special settings may exist for this field).

Practice: Retrieve associated tags by document ID

Now, let's look at how to apply it in different scenarios through specific code examplesitemIdParameter.

Scenario one: Get the associated tags of the current document

Suppose you are editing a template for an article detail page (for examplearchive/detail.html),You want to display all the tags associated with the current article at the bottom of this page. At this time, you can write the code like this:

<div class="article-tags">
    <h4>文章标签:</h4>
    {% tagList tags %} {# 在文档详情页,未指定itemId时会自动获取当前文档的标签 #}
        {% if tags %} {# 判断是否有标签存在 #}
            <ul>
            {% for item in tags %}
                <li><a href="{{ item.Link }}" title="{{ item.Title }}">{{ item.Title }}</a></li>
            {% endfor %}
            </ul>
        {% else %}
            <p>该文章暂无关联标签。</p>
        {% endif %}
    {% endtagList %}
</div>

In this code block,{% tagList tags %}Not explicitly passeditemId,AnQi CMS will intelligently parse it to obtain the associated tags of the current document, thenforloop through and display the title and link of each tag.

Scenario two: Retrieve associated tags by specifying document ID

Sometimes, you may need to retrieve information on a non-document detail page, or you may want to retrieve information on a document detail pageotherA specific document's associated tags. For example, in a sidebar recommendation area, you want to display all the tags of a popular article (assuming ID 123).

First, you need to make sure to get the ID of the document. You can use{% archiveDetail %}tags to get the ID of the specified document, and then pass this ID to{% tagList %}.

<div class="featured-article-tags">
    <h4>热门文章“AnQiCMS功能解析”的标签:</h4>
    {# 先获取热门文章(ID为123)的ID #}
    {% archiveDetail featuredArticle with name="Id" id="123" %}
        {% set articleId = featuredArticle %} {# 将文档ID赋值给一个变量 #}
    {% endarchiveDetail %}

    {# 再使用这个变量ID来获取关联标签 #}
    {% tagList tags with itemId=articleId %}
        {% if tags %}
            <ul>
            {% for item in tags %}
                <li><a href="{{ item.Link }}" title="{{ item.Title }}">{{ item.Title }}</a></li>
            {% endfor %}
            </ul>
        {% else %}
            <p>指定文章暂无关联标签。</p>
        {% endif %}
    {% endtagList %}
</div>

This example demonstrates how to perform chained operations, first byarchiveDetailretrieving the ID of a specified article, then assigning the ID toarticleIda variable, finallytagListTag throughitemId=articleIdThe parameter accurately retrieves all related tags of this popular article.

More application scenarios and considerations

ByitemIdparameter,{% tagList %}The flexibility of tags has been greatly enhanced. You can take advantage of this feature to achieve:

  • Enhance the page relevance of the articleShow related tags at the bottom of the article to guide users to more content of the same topic.
  • Build dynamic content blocksOn the homepage or category page, display tags for specific "recommended articles" or "latest products" to provide more click-through options.
  • Personalized recommendations: Combine user behavior analysis to dynamically load tags of articles that users may be interested in.
  • SEO optimizationThrough displaying related tags, increase the internal link density between pages, and enhance the weight distribution of keywords.

Summary

Of Security CMS{% tagList %}Label collaborationitemIdThe parameter provides powerful tools for website content operation. Whether it is intelligently identifying the current document or accurately specifying any document, it can help you efficiently obtain and display tags associated with the document.Master this skill and you will be able to better organize website content, improve user experience, and optimize the website's search engine performance.

Frequently Asked Questions (FAQ)

  1. Question:itemIdthe parameter to0What effects will there be?Answer: WhenitemIdthe parameter to0then,{% tagList %}The tags will no longer be filtered based on any specific document, but will return the list of all tags on the website.This is usually used to build a tag cloud or tag index page for the entire site, rather than the associated tags of a specific document.

  2. Ask: Can I call tags of articles from other categories on the article detail page?Answer: Absolutely.itemIdThe parameter only cares about the unique ID of the document, not the category it belongs to. So, you just need to know the specific ID of the document you want to retrieve the tag for (regardless of which category it belongs to), and then use it asitemIdthe value is passed to{% tagList %}Just do it.

  3. Question: If a document is not associated with any tags,{% tagList %}what will be displayed?Answer: If a document is not associated with any tags, then{% tagList %}The variable defined by the tag (for exampletags) will be an empty array. In the template, you can use{% if tags %}To determine if the tag list is empty, and display "No tags" or other prompts accordingly to optimize the user experience.