As an experienced website operation expert, I fully understand the importance of tags (Tag) in content management and website optimization.They can not only help users quickly locate the content they are interested in, but also provide search engines with clearer website structure information, thereby improving SEO effects.In a powerful content management system like AnQiCMS, flexibly obtaining and displaying the list of Tag tags for a specified document is a core skill that every operator and developer should master.

Today, let's delve into how to efficiently and accurately obtain the Tag list of a specified document in AnQiCMS.

The positioning and value of tags in AnQiCMS

AnQiCMS was designed from the beginning to fully consider the flexibility of content organization and SEO-friendliness.As described in the project strengths document, the "article, product Tag label feature" is one of its core highlights.When editing documents in the background, we can easily add multiple tags to articles or products. These tags act like an index of topics for the content, organically linking related content scattered across different categories.For example, an article about AnQiCMS Docker deployment can be tagged with "AnQiCMS

Core weapon:tagListTemplate Tag

To obtain the tag list of a specified document in the AnQiCMS front-end template, we mainly rely on a powerful built-in template tag:tagListThis tag allows us to filter and display tags flexibly based on various conditions.

Key parameters:itemIdAccurate positioning

For obtaining the tags of the specified document,tagListThe tag provides a core parameter -itemId. The cleverness of this parameter lies in its ability to precisely specify which document's tag list you want to obtain.

  • Default behavior: For example, on the document detail page (such asarchive/detail.htmlorproduct/detail.html),If you do not specifyitemId,tagListTags will intelligently default to reading the ID of the currently browsing document and display all associated tags.
  • Specify manuallyIf you want to display specific document tags on non-document detail pages (such as the homepage, category list page, or any place where you need to show specific document tags), or if you want to explicitly specify the tags of a document, you can useitemId="文档ID"The format, passing in the unique ID of the target document.
  • Get all tags: If you want to display all the tags on the website without associating with any specific document, you can set theitemIdis set to"0", that isitemId="0".

Practice operation: Get the tag list of the specified document

Now, let's take a common scenario as an example: Display all the tags associated with the current article at the bottom of the article detail page.

  1. Confirm template file: Usually, the template file corresponding to the document detail page will be in your theme directory.archive/detail.htmlorproduct/detail.html.
  2. Get document ID: In the document detail page, all information of the current document will be displayed througharchiveVariable (or any custom document variable name, such asproduct) provided. Therefore, we canarchive.Idto obtain the ID of the current document.
  3. UsetagListTag: We willarchive.IdasitemIdpass the parameter totagListLabel, and you can define a variable name for the label list, for examplearticleTags.

Here is a typical code example of how to retrieve and display the specified document tags in the AnQiCMS template:

<div class="article-tags-section">
    <h4>本文标签:</h4>
    <div class="tag-list-container">
        {# 使用 tagList 标签,通过 itemId 参数指定当前文档的ID来获取其关联标签 #}
        {# limit="10" 表示最多显示10个标签,您可以根据需要调整 #}
        {% tagList articleTags with itemId=archive.Id limit="10" %}
            {# 检查是否有标签,避免空区域显示 #}
            {% if articleTags %}
                {% for tag in articleTags %}
                    <a href="{{ tag.Link }}" title="查看更多关于 {{ tag.Title }} 的文章" class="tag-item">
                        {{ tag.Title }}
                    </a>
                {% endfor %}
            {% else %}
                <span class="no-tags-message">本文暂无相关标签。</span>
            {% endif %}
        {% endtagList %}
    </div>
</div>

{# 如果您希望在其他页面,获取ID为5的文档的标签列表,可以这样使用: #}
{#
<div class="other-document-tags">
    <h5>ID为5的文档标签:</h5>
    {% tagList specificDocTags with itemId="5" limit="5" %}
        {% if specificDocTags %}
            {% for tag in specificDocTags %}
                <a href="{{ tag.Link }}" class="tag-item-small">{{ tag.Title }}</a>
            {% endfor %}
        {% else %}
            <span>该文档暂无标签。</span>
        {% endif %}
    {% endtagList %}
</div>
#}

Code analysis:

  • {% tagList articleTags with itemId=archive.Id limit="10" %}This is the core part. It callstagListTags, store the list of tags obtained inarticleTagsthe variable.itemId=archive.IdEnsure that we are getting the tags of the current page document.limit="10"Limited the number of displayed tags to keep the page concise.
  • {% if articleTags %}: It is a good practice to judge.articleTagsThe variable has content. If the document has no associated tags, it will not display an empty 'This Article Tag' area, but will display a friendly prompt.
  • {% for tag in articleTags %}:TraversearticleTagsEach label object in the list.
  • <a href="{{ tag.Link }}" ... class="tag-item">{{ tag.Title }}</a>For each label, we create a hyperlink.{{ tag.Link }}An automatic URL for the tag detail page will be generated, and clicking on it will allow the user to see all articles tagged with this tag.{{ tag.Title }}It will display the name of the tag. You can also go throughtag.Id/tag.DescriptionMore information can be obtained from the relevant fields (seetag-archiveTag.mdthe document).

Summary

MasteredtagListtags and theiritemIdThe flexible use of parameters allows you to easily obtain and display the Tag tag list of the specified document in AnQiCMS.This can not only make your content organization more professional and systematic, but also significantly improve the user experience in discovering content on the website, thereby indirectly promoting SEO effects.The AnQiCMS template tag system aims to provide high customization and convenience. Making good use of these tools will greatly improve the operation of your website.


Frequently Asked Questions (FAQ)

Q1: Can I directly get the Tag tags of all websites instead of a specific document? A1:Of course you can. If you want to display all tags at any location on the website (for example, in the "Popular Tags" module in the sidebar), you just need totagListlabel'sitemIdthe parameter to"0".{% tagList allSiteTags with itemId="0" limit="20" %}.

Q2:tagListandtagDataListWhat is the difference between these two tags? A2:Their functions focus on different aspects.tagListThe purpose of the label is to obtain.List of tags associated with the specified documentIt returns,Tagan object (including tag names, links, etc.). AndtagDataListThe purpose of the label is to obtain.Document list associated with the specified tagIt returns,ArchiveObject (including document title, link, summary, etc.), usually used on the tag detail page to display all articles under the tag.

Q3: Can I get the tag list of a specified document (such as a document with ID 10) on a non-document detail page? A3:Certainly, it can be done. As long as you know the ID of the target document, you can go through any template file on the website.itemId="10"This way, specify the retrieval of the document tag list. AnQiCMS template tag design allows cross-page data calls as long as the correct