How to get the title list (H1-H6) of document content for generating catalog navigation?

Calendar 👁️ 72

In daily website operations, we often encounter long-form content such as tutorials, product descriptions, or in-depth analysis articles.At this point, it is particularly important to add a clear table of contents to the article (usually located in the sidebar or at the top of the article).It not only helps visitors quickly understand the structure of the article, directly jump to the part of interest, but also improves the SEO performance of the page and enhances the user experience.In AnQiCMS (AnQiCMS), implementing this feature is actually simpler than you imagine, thanks to a very practical template tag provided by the system.


The core feature revelation:ContentTitlesThe clever use of tags

AnQi CMS considers the display of content very carefully and provides a tag namedContentTitlesThe special field. This field is not used to display article content, but is specifically used to extract information about all title levels (H1 to H6) from the article content and provide it to us in a structured data format, which is very suitable for dynamically generating article directory navigation.

When we usearchiveDetailto obtain article details by specifyingname="ContentTitles"The system will intelligently parse the article content, packaging all recognized titles (including title text, HTML tags, levels, and possible prefix numbers) into an array for us to call in the template.

Specific implementation steps

To utilizeContentTitlesTag building directory navigation, usually it can be divided into the following steps:

Step one: Ensure that the article content has a clear title structure

When editing article content on the Anqi CMS backend, whether using the rich text editor to set H1-H6 titles (for example, through the "Set Title/Text" function), or writing content using Markdown editor and using#/##The equal sign defines the title, the system will automatically recognize and save the structural information of these titles. This isContentTitlesThe basis for the label to work properly. Make sure that your article content is organized according to the HTML title specification (H1-H6).

Step two: Call in the template.ContentTitlesGet Title List

Then, in the article detail page template (usually{模型table}/detail.htmlIn the document template you have customized, we need to find the right place to insert the table of contents navigation. For example, you may want it to display in the sidebar of the article text or at the top.

In the template, we can call it like thisContentTitlesField:

{% archiveDetail contentTitles with name="ContentTitles" %}
    {# 此处将接收到一个名为 `contentTitles` 的数组变量 #}
    {# 接下来我们将遍历这个数组来构建目录 #}
{% endarchiveDetail %}

Here, we usearchiveDetailThe tag obtained the current article'sContentTitlesdata and assigned it to a variable namedcontentTitlesto the template. ThiscontentTitlesNow the variable contains all the detailed information of the article's titles.

Step 3: Traverse and build the directory navigation

After obtaining the title data, we just need to use a simpleforloop to build the directory navigation.contentTitlesEach element in the array is an object that contains the following useful properties:

  • TitleThe actual text content of the title.
  • Tag: The HTML tag of the title, for example, "H1", "H2", etc.
  • Level: The level of the title, where the number 1 represents H1, 2 represents H2, and so on.
  • Prefix: If the title has automatic numbering, it will include a prefix (such as "1.1").

Using these properties, we can create a structured list:

{% archiveDetail contentTitles with name="ContentTitles" %}
    {% if contentTitles %} {# 检查是否有标题,避免空目录 #}
        <div class="article-toc">
            <h4>文章目录</h4>
            <ul class="toc-list">
                {% for item in contentTitles %}
                    <li class="toc-item toc-level-{{ item.Level }}">
                        <a href="#{{ item.Title|urlencode|lower|replace:"%20","-" }}" title="{{ item.Title }}">
                            {% if item.Prefix %}{{ item.Prefix }} {% endif %}{{ item.Title }}
                        </a>
                    </li>
                {% endfor %}
            </ul>
        </div>
    {% endif %}
{% endarchiveDetail %}

In the above example, we created a simple unordered list as a directory. Bytoc-level-{{ item.Level }}Such CSS class names, you can easily add different indentation styles to different levels of headings, making the directory structure clear at a glance. At the same time, we have generated a directory item for each one.hrefThe attribute, by usingitem.TitleURL encoding, converting to lowercase, and replacing spaces with hyphens, creates a simple anchor link.

Optimization and Advanced

To make your directory navigation feature more perfect, there are still some optimization suggestions that can be considered:

  1. Implement clickable anchor links: Just generating a directory list is not enough. After clicking on a directory item, the page should smoothly scroll to the corresponding title position. This requires adding a unique identifier to the actual title elements in the article (such as<h2>/<h3>etc.idProperty, and thisidShould be in the directory navigation<a>label'shrefThe properties match. You can dynamically add an ID to the title using front-end JavaScript when the page loads, or use a similar method to generate it on the server sideitem.Title|urlencode|lower|replace:"%20","-"in the mannerid.
  2. Styled CatalogueUtilizeitem.LevelAttribute, you can flexibly add CSS styles to directory items at different levels, for example, by increasing the left margin to simulate a tree structure, which improves the readability and aesthetics of the directory.
  3. Conditional Display of Directory: If the article content is short, or the number of titles is not many, it may not be necessary to display the directory. You can use it in the template.{% if contentTitles|length > 某个数量 %}To determine the number of titles, the table of contents is displayed only when there are enough titles.

By following these steps and optimizations, you can easily generate a complete, beautiful, and practical directory navigation for article content in Anqi CMS, significantly enhancing the user experience and content value of the website.


Frequently Asked Questions (FAQ)

  1. Ask: Why is my article directory navigation not displaying any content?Answer: First, please check if your article content includes H1 to H6 title tags. If the article does not use any titles,ContentTitlesThe label cannot naturally extract the data. Secondly, confirm that you have used the template file correctlyarchiveDetail contentTitles with name="ContentTitles"label, and iterate through the loopcontentTitlesThe code of the variable is correct. Sometimes, cache issues may cause content to not be updated in time, you can try to clear the system cache.

  2. How do I make the directory item click scroll to the corresponding title, rather than just jumping to the top of the page?This requires setting the actual title element in the article content (for example<h2>我是标题</h2>Add a uniqueidproperty. You can use the ID that matches thehrefproperty, for exampleid="我是标题"(URL-friendly). On the front end, you can write JavaScript code to iterate over these titles and generate IDs for them when the page loads, or ensure that the titles have predictable IDs when rendering the article content on the back end.This, in the directory of<a>label to accesshref="#对应的ID"and point to a specific title within the page.

  3. Question:ContentTitlesThe label will get which levels of titles?Answer:ContentTitlesThe tag is very intelligent, it will automatically identify and return all the HTML standard title levels in the article content, that is, from H1 to H6 all the title information. Initem.LevelIn the properties, you will see the corresponding number level, for example, H1 corresponds to 1, H2 corresponds to 2, and so on.

Related articles

How can AnQi CMS enable lazy loading display for images in document content?

In today's online environment, the loading speed of a website is one of the key factors in user experience and search engine ranking.When web page content includes a large number of images, these images often become the 'culprits' that slow down loading speed.Imagine a user opening a page, even if they haven't scrolled to the bottom of the page, the browser has already started downloading all the images, which undoubtedly consumes valuable bandwidth and processing resources, resulting in slow page response.In order to solve this pain point, the Lazy Loading technology for images was born.

2025-11-08

How to retrieve and display the complete content and all fields of a single document?

One of the core values of a content management system is the ability to flexibly define and present various types of content.For users of AnQiCMS, whether it is to display a detailed article, a product page, or a custom data entry, how to efficiently obtain its complete content and all associated fields is the key to front-end template development and content presentation.Today, let's delve into how to easily control the acquisition and display of individual document content in AnQiCMS using this '万能钥匙' template tag.

2025-11-08

How to achieve accurate display of in-site search results in Aiqi CMS?

The importance of on-site search functionality in daily website operation is self-evident.It is not only a bridge for users to quickly find the information they need, but also a key link to improve user experience, extend visit duration, and even promote conversion.If a user enters a keyword and gets a bunch of irrelevant results, they are likely to leave quickly.Therefore, how to achieve accurate display of in-site search results in AnQiCMS is a topic that every content operator should delve into.AnQiCMS is a system specially designed for content management, with a powerful and flexible search mechanism and content organization capabilities

2025-11-08

How to exclude specific category content from the document list?

In website operation, we often need to flexibly control the display of content.For example, you may want to display the latest articles on the homepage, but not include a specific category such as "internal notices" or "archived content";Or in a product list, you want to exclude those categories that are "discontinued" or "internal testing", to keep the user interface simple and clear. A security CMS provides very convenient and powerful functions to meet such needs.By cleverly using the template tag `archiveList`

2025-11-08

How to display the cover image, thumbnail, or multiple image sets of the document in Anqi CMS?

How to display the cover image, thumbnail, or multi-image set of documents in AnQi CMS is a concern for many operators.A visually appealing website cannot do without carefully arranged images.The Anqi CMS offers very flexible and powerful features in this aspect, whether it's setting a prominent cover image for an article, automatically generating thumbnails for list display, or building a rich multi-image collection, it can be easily achieved.

2025-11-08

How to display the publish time, update time, view count, and category of the document?

In AnQiCMS, flexibly displaying content is one of its core advantages, which not only concerns the user experience but also directly affects the website's SEO performance.The document's publication time, update time, view count, and category information are often the focus of users and important indicators for search engines to evaluate the timeliness and relevance of content.It's good that AnQiCMS provides us with intuitive and powerful template tags, making the display of this information very simple.

2025-11-08

How to retrieve and display the Tag list associated with the document in AnQi CMS?

The AnQi CMS provides a flexible and powerful Tag feature in content management, which can not only help you better organize content but also effectively improve the website's SEO performance and user experience.This article will introduce how to manage and retrieve the Tag tag list associated with documents in Anqi CMS and display it on your website front-end. ### Backend Tag Management: Effective Classification and Association Tag management in Anqi CMS is intuitive and convenient.You can find the 'Document Tag' feature under the 'Content Management' menu in the background.

2025-11-08

How to display the links to the previous and next documents on the document detail page?

In website operation, providing a smooth user experience is crucial, and the previous and next navigation on the document detail page is the key to improving user experience and guiding users to deeply browse the website content.In AnQiCMS (AnQi CMS), implementing this feature is simple and efficient, it comes with dedicated template tags that allow you to easily add this practical feature to your website content. ### The Importance of Navigation Between Articles After users finish reading a document, they often want to find related or the next article to get more information.

2025-11-08