How to retrieve and loop to display the title of single-page content (`ContentTitles`) through template tags?

Calendar 👁️ 63

As an experienced security CMS website operator, I am well aware of the importance of content organization and user experience in attracting and retaining users.When dealing with long articles or complex content, it is crucial to clearly present the structure of the content, such as through a dynamically generated table of contents, which is essential for improving user reading efficiency and the SEO performance of the website.The Anqi CMS provides very convenient template tags to help us achieve this goal.

How to use template tags to loop through a list of article content titles (ContentTitles) in AnQi CMS

In Anqi CMS, the article content title list (ContentTitles) is a very practical feature, it allows us to automatically extract the article content (ContentField) of various levels of headings (usually H1-H6 tags), and organize them into a structured array for template iteration.This is of great value for automatically generating article contents, content summaries, or optimizing in-page navigation.

To get and loop displayContentTitlesWe mainly rely onarchiveDetailTemplate tags. This tag is usually used to get the details of the current article or a specified article, andContentTitleswhich is a special field in the returned data.

Firstly, we need to use in the article detail page templatearchiveDetailtags to retrieveContentTitlesfield. This tag can specify a variable name to carry the obtained data, for example, we can name itcontentTitles.

{% archiveDetail contentTitles with name="ContentTitles" %}

In this code block,with name="ContentTitles"This clearly indicates that we want to extract the title list data from the current article details. If you need to retrieve the title list of a specific ID article, you can addid="文章ID"Parameter.

OncecontentTitlesA variable has been successfully assigned, it will contain an array. Each element of this array represents a title in the article, and each element is an object containing the following useful properties:

  • Title: The text content of the title.
  • Tag: The HTML tag used for the title (e.g., "H1", "H2", "H3", etc.).
  • LevelTitle Level (number, H1 is usually 1, H2 is 2, and so on).
  • PrefixPrefix that the system may automatically generate for the title, such as numbering.

In order to display these titles, we can usefora loop tag to traversecontentTitlesan array. Inside the loop, we can useTitle/Tag/LevelandPrefixBuild the display effect we want using properties, such as generating a hierarchical directory.

The following is a detailed code example showing how toContentTitlesAnd loop to display:

<div class="article-toc">
    <h3>文章目录</h3>
    {% archiveDetail contentTitles with name="ContentTitles" %}
        {% if contentTitles %} {# 检查是否有标题存在 #}
            <ul class="toc-list">
            {% for item in contentTitles %}
                {# 根据标题层级添加不同的类或样式 #}
                <li class="toc-item toc-level-{{ item.Level }}">
                    <a href="#{{ item.Title|urlencode }}"> {# 生成锚点链接,这里需要对标题进行url编码 #}
                        <span class="toc-prefix">{{ item.Prefix }}</span>
                        <span class="toc-title">{{ item.Title }}</span>
                    </a>
                </li>
            {% endfor %}
            </ul>
        {% else %}
            <p>本文暂无目录</p>
        {% endif %}
    {% endarchiveDetail %}
</div>

In this example, we first use{% archiveDetail contentTitles with name="ContentTitles" %}Assign the list of article titles to acontentTitlesvariable. Then, by{% for item in contentTitles %}looping through this array. In each loop,itemthe variable represents a title object, and we can access it byitem.Title/item.LevelWait to access its properties. We can even useitem.Levelto dynamically adjust the style of directory items, for example by addingtoc-level-{{ item.Level }}Such CSS classes are used to represent different levels, thereby building a clear and readable article directory.

In this way, the content operators of Anqi CMS can very flexibly control the display structure of article content, which not only improves the reading experience of users in long articles, but also helps search engines better understand the structure of page content, thereby potentially improving the SEO performance of the website.


Frequently Asked Questions (FAQ)

Q1:ContentTitlesHow to extract the title from the article content?A1:ContentTitlesTags will automatically parse the HTML structure of the article text (Contentfield) to identify standard HTML title tags (such as<h1>/<h2>/<h3>/<h4>/<h5>/<h6>)。The system will build based on the text content, tag type, and hierarchy of these tags.ContentTitlesarray. Therefore, make sure that the content of your article uses the correct title tag format, isContentTitlesThe foundation for normal operation.

Q2: Can I controlContentTitlesExtract the title of a specific level, for example, only extractH2andH3?A2: According to the current template tag design,archiveDetailtags are being retrievedContentTitleswill default to extractContentAll levels of headings (H1-H6). You cannot filter specific heading levels directly throughContentTitlesthe tag's parameters. However, you can do so in the template'sforAdd a conditional judgment inside the loop ({% if item.Level == 2 or item.Level == 3 %}) to selectively display or process titles at a specific level.

Q3: If there are no title tags in the article content,ContentTitleswhat will be returned?A3: If the article content (Contentfield) does not contain any HTML title tags (<h1>to<h6>ThenContentTitlesit will return an empty array. In the template, you can use{% if contentTitles %}Such conditional judgment is used to check if the array is empty, thereby deciding whether to display the catalog or give the corresponding prompt, such as 'This article has no catalog'.

Related articles

How does the `render` parameter in the `pageDetail` tag control the conversion of Markdown content?

As an experienced website operator who deeply understands the operation of Anq CMS, I know the impact of content presentation on user experience and the professionalism of the website.In daily content management, we often encounter scenarios where we need to flexibly control content conversion, especially for content written in Markdown format.The Aanqi CMS provides fine-grained control, where the `render` parameter of the `pageDetail` tag is the key.

2025-11-06

How to get the ID of the current single page in the template and display its title with the `pageDetail` tag?

As an experienced CMS website operation personnel of AnQi, I fully understand the core position of content in the website and the importance of template making in the effective presentation of content.When we need to accurately obtain and display the current page ID and title in a single-page detail template, AnQiCMS provides an intuitive and efficient template tag `pageDetail` to complete this task.

2025-11-06

When the website is closed (in shutdown status), can the single page be accessed normally or display a specific prompt?

In the operation practice of Anqi CMS websites, the shutdown (i.e., entering the shutdown state) is a critical step that requires careful handling.When a website needs to temporarily stop providing services due to maintenance, upgrades, or content adjustments, it is crucial to correctly set the shutdown status and display appropriate prompt information, which is important for maintaining user experience and search engine friendliness. ### Overview of the shutdown feature of Anqi CMS Anqi CMS is built with powerful global function settings, including management of the website's operational status.

2025-11-06

Does the single-page support integration with third-party comment systems?

The AnQi CMS offers a powerful and flexible solution in content management, committed to meeting the diverse needs of users.This question of whether the single-page supports integration with third-party comment systems can be explored from two perspectives: the built-in functions of AnQiCMS and the flexibility of its template system.The AnQiCMS system itself indeed has a comprehensive comment management feature.

2025-11-06

How to operate the AnQiCMS multi-site management function?

As a website operator who deeply understands the operation of AnQiCMS, I know the importance of efficient management of multiple sites for corporate brand building and content operation.The multi-site management function of AnQiCMS is specifically designed to meet this need, allowing you to manage multiple independently operated websites under a single system architecture, whether it is a brand sub-site, a multi-language site, or a content branch, it can be easily handled.Now, let's delve into the specific operational process of the AnQiCMS multi-site management feature.

2025-11-06

How to use AnQiCMS custom content model to meet specific business needs?

As a senior CMS website operator, I fully understand the core role of the content management system in business development.A flexible and customizable content management system that can help us efficiently deal with various business scenarios and meet the ever-changing needs of readers.In AnQiCMS, the content model is a powerful tool to achieve this flexibility.The content model is one of the core features of AnQiCMS, allowing us to create a completely personalized content structure based on specific business scenarios and content types.

2025-11-06

How to implement multilingual content switching and display on AnQiCMS

As a senior security CMS website operator, I am well aware of the core role of content in website construction and promotion. Effective management and display of multilingual content is an indispensable part in expanding the international market and serving global users.The AnQi CMS provides a flexible and efficient solution for multilingual support, combining multi-site management with refined template control to help operators easily achieve global content layout.

2025-11-06

How to configure pseudo-static and 301 redirects in AnQiCMS to optimize SEO?

As a senior security CMS website operations personnel, I fully understand the importance of search engine optimization (SEO) for website traffic and user retention.In AnQiCMS, pseudo-static URLs and 301 redirects are two core SEO tools that can help our website perform better in search engines.High-quality content is the foundation for attracting readers, and an excellent URL structure and effective redirection strategy are crucial for ensuring that this content can be discovered by search engines and effectively pass on value.

2025-11-06