How to generate the catalog navigation on the front page for the automatic extraction of article content (`ContentTitles`) feature?

Calendar 👁️ 70

In AnQi CMS, to help readers better understand the structure of long articles, providing a clear table of contents is the key to improving user experience. AnQi CMS automatically extracts titles from the article content (ContentTitlesThis feature was created for this very purpose. It can intelligently identify the various levels of headings in the article content and provide this structured information to the front-end template, thereby allowing us to easily build a dynamic and practical directory navigation.

UnderstandingContentTitlesFeature

ContentTitlesIsarchiveDetailA powerful attribute provided by the tag. When we use it on the article detail pagearchiveDetailLabel to retrieve the current article content, it will not only return the main content, title, description and other information of the article, but will also particularly provide a name forContentTitlesThe field. This field contains all the text identified as titles in the article and their corresponding hierarchy information.

To be specific,ContentTitlesAn array (or list) will be returned, each element of which is an object containing the following information:

  • Title: The text content of the title.
  • Tag: The HTML tag type for headings, for example 'H1', 'H2', 'H3', etc.
  • Level: The level of the title, for example, 1 represents H1, 2 represents H2, and so on. This is crucial for building nested directories.
  • PrefixIf the title has a prefix (such as automatic numbering), it will be included here.

By these structured data, we can precisely know which titles are in the article, as well as their hierarchical relationships, providing a solid foundation for generating the catalog navigation.

Why is the article catalog navigation so important?

The article catalog navigation is not just an aesthetic element, it plays multiple roles in content operation:

  • Improve user experience:Readers can easily see the outline of the article, quickly jump to the chapters of interest, especially for long articles, which greatly reduces the trouble of scrolling to search.
  • Optimize SEO:Search engines prefer content that is structured clearly and easy to crawl. The directory navigation can help search engines better understand the topic and structure of the article, which is helpful for improving the ranking of the content.
  • Increase the page stay time:Convenient navigation makes it easier for users to immerse themselves in the content, reducing the bounce rate.
  • Assist in content creation:Remind the author to pay attention to the title hierarchy and logic when writing articles, so as to produce higher-quality content.

Steps to generate a directory navigation on the front-end page.

To utilizeContentTitlesThe function generates directory navigation on the front end, mainly involving the following steps:

  1. Obtain in the article detail templateContentTitlesData:Firstly, we need to be in the article detail page template file (usually{模型table}/detail.htmlUse it inarchiveDetailTag to get the list of article titles.

    {% archiveDetail contentTitles with name="ContentTitles" %}
        {# contentTitles 变量现在包含了文章中所有标题的结构化数据 #}
    {% endarchiveDetail %}
    

    Here, we assign the extracted title data to.contentTitlesthis variable.

  2. the judgment.ContentTitlesIs empty and start building the directory structure:After obtaining tocontentTitlesAfter the variable, we usually check if it contains any titles.If the article does not detect a title (for example, only plain text is used), then the table of contents navigation does not need to be displayed.If there is a title, we can start using HTML's<ul>and<li>Tag to build the list structure of the catalog.

    {% archiveDetail contentTitles with name="ContentTitles" %}
        {% if contentTitles %}
        <nav class="article-toc">
            <h4>文章目录</h4>
            <ul class="toc-list">
                {# 在这里循环生成目录项 #}
            </ul>
        </nav>
        {% endif %}
    {% endarchiveDetail %}
    
  3. Traverse the title data and create clickable catalog items:Next, we need to useforto loop throughcontentTitlesOf each title object, and generate a list item for each title. At the same time, in order to achieve smooth page jumping to the corresponding title position when clicking on the catalog item, we need to create an anchor link for each catalog item (<a>Label), and points to the corresponding title in the article contentidProperty.

    A common practice is that when we insert H1, H2, and other titles into the rich text editor of an article, the editor will automatically generate a uniqueidattribute (for example,id="section-introduction")。If your editor does not have this feature, you may need to manually ensure that the HTML titles (such as<h2 id="section-introduction">引言</h2>)are uniqueid.

    When generating directory items, we can base them onitem.Title(Title Text) anditem.LevelTo dynamically generate aid. For example, you can build it like this:toc-+标题文本+-+层级comma and process the title text by replacing spaces with hyphens, converting to lowercase, making it URL-friendly.

    {% archiveDetail contentTitles with name="ContentTitles" %}
        {% if contentTitles %}
        <nav class="article-toc">
            <h4>文章目录</h4>
            <ul class="toc-list">
                {% for item in contentTitles %}
                    {# 生成一个URL友好的唯一ID,例如:'toc-文章导读-1' #}
                    {# 注意:您需要确保文章内容中的对应标题元素也有相同的ID属性,才能实现点击跳转 #}
                    {% set sectionId = 'toc-' ~ item.Title|replace:" ","-"|lower ~ '-' ~ item.Level %}
                    <li class="toc-item level-{{item.Level}}">
                        <a href="#{{ sectionId }}">{{item.Prefix}} {{item.Title}}</a>
                    </li>
                {% endfor %}
            </ul>
        </nav>
        {% endif %}
    {% endarchiveDetail %}
    

    This has been usedreplace:" ","-"|lowerA filter to replace spaces in the title with hyphens and convert it to lowercase to generate a more standardized HTML ID.~The symbol is used to concatenate strings.

  4. Add style and interaction (optional but recommended):After completing the HTML structure, you can add style to the directory navigation using CSS to make it more visually appealing. For example, according tolevel-XThe class name indents the titles at different levels. You can also choose to use a JavaScript library or a custom script to implement some advanced interactive features such as:

    • Smooth scrolling:Click on the catalog item to smoothly scroll to the corresponding title.
    • Highlight the current reading chapter:When the user scrolls the page, the corresponding title item in the directory will be highlighted to indicate the current reading position.
    • Collapse/Expand directory:Provide a collapse/expand button for long directories to keep the page tidy.

Complete code example (HTML/Twig part only):

This is your article in a JSON object

Related articles

How to implement lazy loading of images in the `archiveDetail` tag of AnQi CMS?

In today's content is king era, the importance of website performance and user experience is self-evident.Especially for content pages with many images, how to avoid affecting user experience and search engine rankings due to slow image loading has become a common concern for website operators.AnQi CMS is well-versed in this, not only providing flexible and efficient content management functions, but also cleverly supporting image lazy loading (Lazy Load).

2025-11-09

How to display and truncate the Description field of AnQi CMS on the front page?

In the daily operation of AnQi CMS, the Description field plays a crucial role, not only affecting how search engines understand the page content, but also being the key copy that attracts users to click.Understand how this field displays and flexibly truncates on the front-end page, which can help us better perform content layout and SEO optimization. ### Description of field in various aspects and functions In AnQi CMS, the "description" field is not a single concept. It will show different forms and functions according to the content type and usage scenario. The most common and has the greatest impact on SEO

2025-11-09

How to implement SEO optimization display of article titles in Anqi CMS and differentiate them from regular titles?

How to implement SEO optimization display of article titles in AnQiCMS, distinguishing from the regular title?In the practice of content operation, we often face a challenge: how to balance the 'readability' of content with 'search engine friendliness'.A captivating title can catch the reader's attention and increase click-through rate;A title containing core keywords can help search engines better understand the content and improve rankings.AnQiCMS fully understands the importance of this requirement and therefore provides a flexible mechanism

2025-11-09

How to customize the content model in Anqi CMS to achieve personalized product display?

When building a successful website, especially for enterprises that need to display a variety of products, the personalization and precise presentation of content are crucial.Traditional website content management systems often provide fixed or limited content types, which may not be sufficient when facing products with different attributes and display requirements.AnQiCMS (AnQiCMS) understands this pain point, through its powerful customizable content model function, it provides users with flexible and efficient solutions, making personalized product display easily accessible.### AnQi CMS Content Model

2025-11-09

How to display the cover image (Logo) and thumbnail (Thumb) of an AnQi CMS article?

In content operation, the cover image (Logo) and thumbnail (Thumb) of the article play a crucial role. They not only attract readers' attention and increase click-through rates, but are also an indispensable part of search engine optimization (SEO).AnQiCMS (AnQiCMS) provides users with a flexible and convenient way to manage and display these visual elements.This article will discuss in detail how to configure and call the cover image and thumbnail of the article in AnQiCMS.## One, Configuration and generation of cover image and thumbnail In AnQiCMS

2025-11-09

How to display the publication time of articles in Anqi CMS and customize the date and time format?

When operating website content, the publication time of the article is an indispensable element.It not only helps visitors quickly understand the "freshness" of the content, but also has a positive impact on search engine optimization (SEO), as it conveys the signal that the website content is continuously updated.AnQiCMS provides great flexibility in content management, whether it is to obtain or customize the display time of published articles, it is very convenient.Next, we will explore how to flexibly display the publication time of articles in Anqi CMS

2025-11-09

How to display the category information of the articles on the front page of AnQi CMS, including the category name and link?

In Anqi CMS, the display of article category information on the front page is a key link in content organization and user navigation.Whether it is to help users understand the context of the article or to optimize the website's SEO structure, accurate and accessible category names and links play an important role.AnQi CMS with its flexible template engine makes this task intuitive and efficient.Understanding Anqi CMS's Classification System Before delving into the display methods, we first need to know that Anqi CMS's content management system attaches great importance to the concept of 'classification'.Each document (whether it is an article

2025-11-09

How to use the `tagList` tag to display the associated tag list on the article detail page?

In content operation, tags (Tags) are a very practical tool that can help us organize content more flexibly, making it convenient for readers to quickly find related topics of interest.Imagine that after a user reads an excellent article, if they can immediately see other popular topics or keywords related to that article, it will undoubtedly greatly enhance their browsing experience and encourage them to explore other content on the website.In Anqi CMS, implementing this feature is very intuitive and simple, we mainly rely on the powerful `tagList` tag to complete it.

2025-11-09