How to use template tags to dynamically display the associated Tag list in article content?

Calendar 👁️ 74

How to let users see the list of related keywords or themes when browsing an article on a website, so that they can discover more interesting content and it is an important factor to improve user experience and website stickiness.AnQiCMS provides powerful template tag features, allowing you to easily display related Tag lists in article content, both beautiful and practical.

Understanding Anqi CMS Tag Functionality

In AnQi CMS, tags (Tag) are not just a simple classification of articles, but more like a keyword index spanning different categories and content models.When you publish articles in the background, you can add multiple tags to the articles.These tags are independent of the classification system, and can cleverly connect articles related to the theme across different categories, even different content models (such as articles and products).

By adding tags to the article, you can not only help users quickly locate relevant content, but also provide search engines with richer semantic information, optimize the internal link structure of the website, and thus improve the inclusion and ranking of the article.

Core: How to get the associated Tag list in an article?

To dynamically display the associated Tag list in the article content, we need to use Anqi CMS'stagListTemplate tag. This tag is specifically used to obtain the tag data associated with a specified article.

tagListThe key to the tag lies initemIdParameter. When you use it on the article detail page (i.e., the page that displays the content of a single article)tagListyou can go throughitemIdParameters to specify the current article ID, thus accurately retrieving all tags associated with the article.

Each fromtagListThe tag objects obtained from it, all contain the title of the tag (Title) and the links(LinkWe can use this information to build a clickable tag list on the page.

Gradually implement: Display the Tag list on the article detail page.

Now, let's look step by step at how to implement this feature in the template file of the article detail page.

  1. Are you sure you want to modify the template file?Generally, we need to perform operations in the template file displaying article details. In AnQi CMS, this is usually under the custom content model directory you have created.detail.htmla file, for examplearticle/detail.htmlorproduct/detail.html.

  2. Locate the insertion positionYou can choose to place the Tag list at the bottom of the article content, as a 'related reading' or 'keywords' area, or in the sidebar, depending on the overall layout and user habits of your website.

  3. Write template codeBefore yourdetail.htmlIn the template file, find the location where you want to display the Tag list, and then insert the following code snippet:

    Firstly, we need to ensure that we get the ID of the current article. The data of the current article is usually loaded in the article detail page.archiveWe can use the variable directly.archive.Id. If for some reason,archivethe variable is not available, you can also get the current article ID througharchiveDetailan explicit tag to get the current article ID.

    {# 假设您正在文章详情页的模板(例如 article/detail.html 或 product/detail.html)中操作 #}
    {# 确保获取到当前文章的ID。在详情页,archive.Id 通常是可用的。 #}
    {# 如果不确定,也可以显式获取:{% archiveDetail currentArticleId with name="Id" %}{% set currentId = currentArticleId %}{% endarchiveDetail %} #}
    {# 或者更简洁地直接使用 archive.Id #}
    {% set currentArticleId = archive.Id %} 
    
    {# 使用 tagList 标签获取与当前文章关联的Tag列表 #}
    {# itemId 参数指向当前文章的ID,limit="10" 限制最多显示10个标签 #}
    {% tagList articleTags with itemId=currentArticleId limit="10" %}
    
    {# 判断是否有标签需要显示 #}
    {% if articleTags %}
    <div class="article-tags-section">
        <span class="section-title">相关标签:</span>
        <ul class="tag-list">
        {% for tag in articleTags %}
            <li class="tag-item">
                <a href="{{ tag.Link }}" title="查看更多关于{{ tag.Title }}的文章">
                    {{ tag.Title }}
                </a>
            </li>
        {% endfor %}
        </ul>
    </div>
    {% else %}
    {# 如果当前文章没有关联任何标签,可以显示一个提示信息 #}
    <div class="no-tags-found">
        本文暂无相关标签。
    </div>
    {% endif %}
    
    {% endtagList %}
    

    In this code block:

    • {% set currentArticleId = archive.Id %}: We got the current article ID and assigned it tocurrentArticleIdVariable. On the article detail page,archiveVariables usually contain all the information of the current article.
    • {% tagList articleTags with itemId=currentArticleId limit="10" %}: This is the core part.tagListThe label will be based onitemId(i.e., the current article ID) query all tags associated with it from the database. We will store the query results inarticleTagsthe variable, and uselimit="10"to limit the maximum number of tags displayed to 10.
    • {% if articleTags %}: We first judgearticleTagsWhether the variable is empty, ensure that the tag list area is rendered only when there are tags, to avoid displaying empty content.
    • {% for tag in articleTags %}: If there are tags, we useforLoop througharticleTagsEach label in the array.
    • {{ tag.Link }}and{{ tag.Title }}: inside the loop,tag.LinkIt will output the URL address of the label,tag.TitleThen output the name of the label. We use<a>tags to wrap them, so they can be clicked to jump to the aggregation page of the tag.
  4. Save and view the effectSave your template file, refresh the article detail page, and you will see a dynamically generated list of associated tags.

Optimization and Advanced

  • Limit the number: As shown in the example, throughlimit="N"Parameters, you can control the maximum number of tags displayed. This is very useful when designing page layouts, as it can prevent the page from becoming lengthy due to too many tags.
  • No tag processingWe have already included in our code{% else %}Part, when the article does not have related tags, it will display a friendly prompt instead of a blank page, improving the user experience.
  • Style beautification: Make sure to add CSS styles to elements such as setting background color, border, rounded corners, font size, and color to make the tag list more consistent with the visual style of your website.div.article-tags-section/ul.tag-listandli.tag-item aetc., for example setting background color, border, rounded corners, font size, and color.
  • SEO-friendly: Tags generated dynamically, which are very beneficial for the internal link structure of the website and SEO optimization.It can help search engine spiders better discover relevant pages within the website, increasing the page authority transfer.

By following these steps, you can dynamically display the associated Tag list in the article content of AnQi CMS efficiently and flexibly.


Frequently Asked Questions (FAQ)

Why does my article detail page, label

Related articles

How to configure AnQiCMS to enable lazy loading of images on the page?

In today's rich network world, the speed of website loading is of great importance to user experience and search engine optimization (SEO).Especially when a page contains a large number of images, how to effectively manage the loading of these images, avoid the lag caused by loading all resources at once, has become a focus of many website operators.The Lazy Load (Lazy Load) technology is the tool to solve this problem, it allows images to be loaded only when the user is about to see them, thus significantly improving page performance.AnQiCMS as a high-performance content management system

2025-11-09

How to display only articles of specific categories in the article list?

When managing website content in Anqi CMS, we often need to flexibly control the display of article lists, for example, only displaying articles under specific categories on a certain page.This not only helps to organize the content of the website accurately, but also greatly improves the browsing experience of visitors, helping them find the information they are interested in faster.AnQi CMS provides powerful template tag features, making this requirement simple and intuitive.The core of achieving this goal lies in the `archiveList` template tag of Anqi CMS.This tag is used to obtain the 'universal key' for the article list from the database

2025-11-09

Does AnQiCMS support displaying different detail page styles based on different content models?

In website operation, we often encounter such needs: different types of content, even on the same website, also need to have completely different display styles.For example, a product detail page of an e-commerce website needs to highlight product images, prices, inventory information, etc., while the blog article detail page may focus more on the reading experience, author information, and publication date.This ability to customize the style of detail pages based on content type (also known as content model) is crucial for improving user experience and content professionalism.Then, while using AnQiCMS

2025-11-09

How to use AnQiCMS to customize the display layout of the article detail page?

AnQiCMS provides a very flexible and powerful content management system, allowing us to easily customize the display layout of article detail pages.In order to enhance user experience, optimize SEO, or better showcase the brand characteristics, AnQiCMS can help us achieve these goals through its intuitive template system and rich tag features.How can we proceed specifically? We can delve into these aspects step by step.### 1. Understanding AnQiCMS template mechanism First

2025-11-09

How does the multi-site feature of AnQiCMS affect the display and switching of front-end content?

When using a website content management system, the multi-site feature has always been a topic of concern.It not only concerns the operational efficiency, but also directly affects the organization, display of website content, and the smooth switching experience of users between different sites.For AnQiCMS users, its powerful multi-site feature is the tool to solve these challenges.Let's delve into how the multi-site feature of AnQiCMS affects the display and switching of front-end content.One of the design初衷 of AnQiCMS is to meet the needs of users managing multiple brands, sub-sites, or multilingual content

2025-11-09

How to use AnQiCMS's pseudo-static rules to optimize the display structure of article URLs to improve SEO?

In website operation, the URL is not only the entrance for users to access content, but also an important basis for search engines to understand and crawl website content.A clear and semantically friendly URL can significantly improve user experience and website performance in search engines.One of the key factors to achieve this goal is to skillfully use pseudo-static technology.AnQiCMS as a high-efficiency and customizable content management system, fully considers the needs of SEO optimization, and provides powerful and flexible pseudo-static rule configuration functions. Today

2025-11-09

How to call and display the title and link of the previous and next articles on the article detail page?

In AnQiCMS, adding navigation for the previous and next articles on the article detail page is an important step to improve the user reading experience and optimize the internal link structure of the website.This feature not only guides readers to continue browsing related content, but also helps search engines better understand the website structure.AnQiCMS's powerful template system provides a very convenient way to meet this requirement.### AnQiCMS Template Basics Review The AnQiCMS template system is simple and efficient, using syntax similar to Django, allowing developers to build pages intuitively

2025-11-09

Does AnQiCMS support displaying the number of articles under each category on the category list page?

When operating a website, we often encounter such needs: when displaying the website category list, we hope to see intuitively how many articles are under each category.This not only helps visitors quickly understand the richness of a topic's content, improve user experience, but also enables us content operators to better plan content strategies, and even has unique value in SEO optimization. Then, does AnQiCMS support displaying the number of articles under each category on the category list page?The answer is affirmative, and it is very convenient to implement. In the template design of AnQiCMS

2025-11-09