As a senior CMS website operation personnel for a security company, I am well aware of the importance of content tags (Tags) in website content organization, SEO optimization, and user experience.They can not only help readers quickly find the content they are interested in, but also enhance the internal links of the website and improve the efficiency of search engine crawling.tagListThe label is the core tool we use to achieve this goal.

This article will discuss in detail how to flexibly use the Anqi CMS template.tagListLabel, to obtain all tags in the website, or accurately obtain the tags associated with a specific document, helping you build a more intelligent and user-friendly website content display.

Understanding the content tags in AnQi CMS

In AnQi CMS, content tags (Tags) are a flexible way to classify content. Unlike traditional categories (Category), tags are usually more granular, and a document can be associated with multiple tags, and a tag can also be associated with multiple documents.This makes the relationship between content more rich and interconnected.

The backend of Anqi CMS provides a comprehensive tag management feature, where you can add, edit, and delete operations under the 'Content Management' module's 'Document Tags'.Each tag can be set with a name, index letter, custom URL, SEO title, keywords, and description. This information can be called from the front-end template to build a beautiful and informative tag page.The main function of the label is to provide an entry for quick browsing of related content, enhance the navigation experience of users within the website, and is also an important part of keyword layout in SEO strategy.

tagListCore functions of template tags

In the template language of AnQi CMS,tagListis a powerful tag, specially used to obtain content tags data within a website.It allows us to query and display these tags in various ways, whether it is to list all tags or to display only tags related to specific content.

tagListThe basic syntax structure of tags is:{% tagList 变量名 with 参数 %}," and is{% endtagList %}End.tagListandendtagListThe content between them will be processed for each loop of tag data.变量名This is the name specified for the label list we obtain, for exampletagsIt will be an array object, and we need to iterate through it using a loop to output each label.forto output each label.

Get the list of all content tags on the website

To get all available content tags on the website, we can usetagListTagsitemIdparameters. Usually,itemIdThe parameter will try to read the document ID of the current page by default to obtain related tags. However, if we want to obtain all tags of the entire website rather than tags of a specific document, we need to explicitly specifyitemIdset"0".

Moreover, in order to ensure that we can obtain a sufficient number of tags, we can adjustlimitParameter.limitThe parameter specifies the maximum number of invocation tags, usually 10 by default. If your website has many tags, it is recommended to setlimitto a larger value, such as"100",with coverage of most cases, or uselimit="1,100"ofoffsetpattern to get a wider range.

The following is an example of getting all tags of a website and displaying their names and links:

<div>
    <h3>所有网站标签:</h3>
    {% tagList allSiteTags with itemId="0" limit="100" %}
        {% if allSiteTags %}
            <ul class="tag-cloud">
                {% for item in allSiteTags %}
                    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <p>目前还没有任何标签。</p>
        {% endif %}
    {% endtagList %}
</div>

In this example,itemId="0"indicatetagListDo not attempt to associate any specific document, but obtain tags at a global level.limit="100"Ensure that no more than 100 tags will be retrieved. We have also added{% if allSiteTags %}Judgment, to handle data without tags elegantly.

Get the tag list of the specified document

When we are on a document detail page, or need to display related tags for a specific document,tagListTagsitemIdthe parameter plays a role again.

If you want to get the tags associated with the current document (i.e., the document the user is browsing), you usually do not need to set them explicitlyitemIdthe parameters, becausetagListlabels are not specifieditemIdWhen, it will default read the document ID of the current page.However, it is good practice to explicitly pass the current document ID for clarity and to avoid potential confusion.archiveDetailTag retrieval.

Assuming we are on a document detail page, and we can accessarchive.IdAccess the ID of the current document:

{# 假设archive.Id是当前文档的ID #}
<div>
    <h3>当前文档的标签:</h3>
    {% tagList currentDocTags with itemId=archive.Id limit="10" %}
        {% if currentDocTags %}
            <div class="document-tags">
                {% for item in currentDocTags %}
                    <span class="tag-item"><a href="{{item.Link}}">{{item.Title}}</a></span>
                {% endfor %}
            </div>
        {% else %}
            <p>该文档未关联任何标签。</p>
        {% endif %}
    {% endtagList %}
</div>

If you need to gettags of a non-current documentjust need toitemIdThe parameter is set to the specific ID of the document. For example, to get all tags of the document with ID 5:

<div>
    <h3>ID为5的文档标签:</h3>
    {% tagList specificDocTags with itemId="5" limit="10" %}
        {% if specificDocTags %}
            <div class="document-tags">
                {% for item in specificDocTags %}
                    <span class="tag-item"><a href="{{item.Link}}">{{item.Title}}</a></span>
                {% endfor %}
            </div>
        {% else %}
            <p>ID为5的文档未关联任何标签。</p>
        {% endif %}
    {% endtagList %}
</div>

This way, you can control preciselytagListThe tag range obtained by the label, whether it is for the entire website or a specific content.

Filter and display tags of a specific category

tagListthe label also supportscategoryIdParameters to further filter tags. This means you can only display those tags associated with documents under specific categories. If you need to display tags for a specific category, you can do so bycategoryId="分类ID"Specify, multiple category IDs can be separated by English commas.,.

For example, to display the tags of all documents associated with category ID 1 and 2:

<div>
    <h3>分类ID 1和2的标签:</h3>
    {% tagList categorySpecificTags with itemId="0" categoryId="1,2" limit="50" %}
        {% if categorySpecificTags %}
            <ul class="filtered-tags">
                {% for item in categorySpecificTags %}
                    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <p>这些分类下没有找到相关标签。</p>
        {% endif %}
    {% endtagList %}
</div>

Here we still useitemId="0"To indicate that it is not limited to a specific document, but from all documents that meetcategoryIdAggregate tags from documents that meet the conditions.

The tag fields available in the loop body

In{% for item in tags %}in the loop,itemThe variable represents the current label object. It provides multiple properties for us to call and display, to build rich label displays:

  • item.Id: The unique ID of the label.
  • item.Title: The display name of the label, which is the most commonly used attribute.
  • item.Link: The page link corresponding to the label, which will jump to the document list under the label when clicked.
  • item.Description: The description information of the label, which can be used for SEO or mouse hover hints.
  • item.FirstLetter: The index letter of the label name (A-Z).
  • item.CategoryId: If the label is associated with a specific category, the category ID will be displayed here.

You can flexibly combine these fields within the loop according to your design requirements to display label information.

Combine pagination to display label lists

For creating an independent label archive page (such as)tag/index.html), we may need to display a large number of labels with pagination.tagListTags intag/index.htmlIn the template, you can set bytype="page"Support pagination feature. AndarchiveListSimilar, the navigation after pagination can bepaginationrendered with tags.

{# 假设在 tag/index.html 模板中 #}
<div>
    <h2>网站标签云</h2>
    {% tagList tags with type="page" limit="50" %} {# 每页显示50个标签 #}
        {% if tags %}
            <ul class="tag-cloud-paginated">
                {% for item in tags %}
                    <li><a href="{{item.Link}}">{{item.Title}}</a></li>
                {% endfor %}
            </ul>
        {% else %}
            <p>抱歉,目前没有可显示的标签。</p>
        {% endif %}
    {% endtagList %}

    {# 标签列表的分页导航 #}
    <div class="pagination-controls">
        {% pagination pages with show="5" %}
            <a class="{% if pages.FirstPage.IsCurrent %}active{% endif %}" href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a>
            {% if pages.PrevPage %}
            <a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a>
            {% endif %}
            {% for item in pages.Pages %}
            <a class="{% if item.IsCurrent %}active{% endif %}" href="{{item.Link}}">{{item.Name}}</a>
            {% endfor %}
            {% if pages.NextPage %}
            <a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a>
            {% endif %}
            <a class="{% if pages.LastPage.IsCurrent %}active{% endif %}" href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a>
        {% endpagination %}
    </div>
</div>

In this example,type="page"informtagListPrepare pagination data required,paginationTags are responsible for rendering standard page navigation.

Template Integration **Practice

  • Define clear variable names: FortagListLabel with clear variable names (likeallSiteTags,currentDocTags), to improve code readability.
  • Handle empty dataAlways use:{% if 变量名 %}or{% empty %}A clause is used to handle the case where the tag list is empty, to avoid the page from displaying blank or error.
  • Performance considerations:Although the tag query efficiency of AnQi CMS is very high, in some special cases, if you need to display hundreds or even thousands of tags on a single page, please pay attention to the page loading performance, and consider whether it is really necessary to display so many tags. For the common 'tag cloud' function, a reasonablelimitThe value is usually between 50-100.
  • Multi-site compatibility: If you have enabled the multi-site feature of Safe CMS and need to call tags between different sites, make sure to use them correctly.siteIdthe parameter to specify the target site.

Through the detailed instructions and examples above, I believe you have gained an understanding of the security CMS template intagListThe usage of tags has been fully understood. Mastering these skills will make you more adept at operating the website of Anqi CMS, creating more attractive and functional content displays.


Common Questions and Answers (FAQ)

1. Why do I only show a few tags instead of all the tags I expected after using the tag?tagListWhat is the limitation of the default value of the parameters?

This is usually due tolimitWhy do I only show a few tags instead of all the tags I expected after using the tag?tagListlabels are not specifiedlimitWhen the parameter is set, the default usually only displays 10 tags. If you want to get more tags, even all tags, please make sure to setlimitthe parameter to a sufficiently large number, for examplelimit="100"Or adjust according to your actual tag number. At the same time, if the goal is to getalltags, make sure you have also setitemId="0"to avoid being limited to the tags in the current document.

2.tagListIs it possible to get empty tags that are not associated with any documents?

According to the design of Anqi CMS, tags are usually associated with document content.tagListThe label is mainly used to retrieve those tags that have been referenced by documents or created in the background and are available. If a tag is completely unrelated to any document and its background management status is not activated, it may not betagListThe label search is found. The significance of the label is usually to aggregate content, so the 'empty label' may not be the target of front-end display in most cases.

3. Can I sort the tags by their usage frequency?tagListSort the returned tags?

tagListThe label parameters currently do not provide a direct option to sort by usage frequency (for example, the number of associated documents). It mainly supports sorting throughlimit/letterandcategoryIdFilter. If you need to sort by tag frequency, this may require custom development in the backend logic of AnQi CMS, or by obtaining all tags via JavaScript on the frontend, and then sorting according to the tags associated with