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

Calendar 👁️ 61

When browsing articles on a website, navigation links such as 'Previous' and 'Next' often appear at the bottom.This seemingly simple detail actually has a significant impact on user experience and the coherence of website content.It can not only guide visitors to continuously explore more content, help to increase the average page visit time, but also optimize the internal link structure of the website.For those using AnQiCMS, implementing this feature is much simpler than one might imagine, as it comes with powerful template tags that make it easy to dynamically retrieve and display this navigation information.

Core feature introduction: Dynamically obtain the secret of adjacent articles

AnQi CMS provides high flexibility in template development, especially for content like articles. It includes two very practical tags:prevArchiveandnextArchiveThese tags can intelligently recognize the current article and automatically find the previous and next articles in the same category or chronological order.This means you do not need to manually maintain the complex relationships between articles, the system will do it automatically for you.

How to implement previous/next article navigation on the article detail page

To integrate the 'Previous' and 'Next' navigation functions into your Anqi CMS website, you need to edit the template file of the article detail page. Usually, this template file will vary depending on your content model and naming conventions, for example, it may be locatedtemplate/您的模板名称/article/detail.htmlortemplate/您的模板名称/产品模型名称/detail.html.

Find and open the article detail template file currently in use on your website, and then you can add the following code snippet below the article content, in the sidebar, or in the location that you think is most suitable for user reading habits:

<div class="article-navigation">
    <div class="prev-article">
        {% prevArchive prev %}
        {% if prev %}
        <span class="nav-label">上一篇:</span><a href="{{ prev.Link }}" title="{{ prev.Title }}">{{ prev.Title }}</a>
        {% else %}
        <span class="nav-label">上一篇:</span><span>没有了</span>
        {% endif %}
        {% endprevArchive %}
    </div>
    <div class="next-article">
        {% nextArchive next %}
        {% if next %}
        <span class="nav-label">下一篇:</span><a href="{{ next.Link }}" title="{{ next.Title }}">{{ next.Title }}</a>
        {% else %}
        <span class="nav-label">下一篇:</span><span>没有了</span>
        {% endif %}
        {% endnextArchive %}
    </div>
</div>

Code Parsing and Explanation:

  • {% prevArchive prev %}and{% endprevArchive %}:This is a tag block specifically used to retrieve the previous article information. It will try to find the previous article in the context of the current article and assign all its data (including title, link, ID, etc.) to the one you defineprevVariable.
  • {% nextArchive next %}and{% endnextArchive %}:withprevArchiveSimilar, this tag block is used to get the information of the next article and assign all its data tonextVariable.
  • {% if prev %}and{% if next %}:These conditions are crucial. Not all articles have a previous or next article (for example, the first article in a category does not have a previous one, and the last article does not have a next one).Therefore, before displaying the content, we need to checkprevornextDoes the variable successfully retrieve the data. If the variable exists, it will display the corresponding title and link;If not present, it can elegantly display 'None' or completely hide the navigation item.
  • {{ prev.Link }}and{{ prev.Title }}: prevandnextVariables are essentially article objects, they contain many properties of the article. Here, we usedLink(the URL link of the article) andTitle(Article title) to build navigation. You can also access other fields mentioned in the document, such asId(article ID),Keywords/Description/Thumb(Thumbnail) and choose to display according to your design needs.

Description of the working principle.

When your website visitor clicks to enter an article detail page, the Anqie CMS backend system will intelligently find the adjacent previous and next article records in the database by combining the current article ID, its category, publication time, or custom sorting in the backend.prevArchiveandnextArchiveThe tag takes advantage of this efficient mechanism to directly pass the structured data retrieved (including article titles, links, etc.) to the front-end template.This design eliminates the trouble of manually managing the complex associations between articles, making the navigation function automatic.

Additional hints and **practice

  1. Confirm template file: Please make sure you are editing the correct article detail template file. If you have enabled a custom content model, then the path of the detail page template may followtemplate/您的模板名称/{模型table}/detail.htmlthe naming rules.
  2. Content sorting logic:The logic for determining the "Previous" and "Next" articles is usually based on the chronological order of the article's publication, or on the custom sorting set in the "Document Management" section of the Anqi CMS backend.If you find that the navigation order does not meet your expectations, it is recommended to check the publication date of the article and the sorting settings on the back end.
  3. Clear the cache:After any modifications are made to the template file, in order to ensure that your changes take effect immediately and are displayed on the website front-end, please log in to the Anqi CMS backend and use the "Clear Cache" feature to clear the website cache.

By following these concise steps, you can easily implement a feature-rich navigation for the previous and next article pages in the Anqi CMS article detail page, thereby significantly improving the website's user experience and content navigation efficiency.


Frequently Asked Questions (FAQ):

Q1: Why did I follow the steps to add the code, but the 'Previous' or 'Next' still shows 'None', even though I am sure there are adjacent articles?

A1: There may be several reasons for this situation. First, check if the article you are currently viewing is the first or last in a category, which naturally leads to a missing navigation direction.Secondly, if the publication status of adjacent articles is not "published" or the publication time is abnormal (for example, in the future), the system may not be able to recognize it correctly.Please check the publication date and status of the relevant articles to ensure they are all normal.Finally, after modifying the template file, please make sure to log in to the AnQi CMS backend, click the "Update Cache" feature, and clear all website caches to ensure that the code update takes effect.

Q2:prevArchiveandnextArchiveTags are determined by which conditions to judge and retrieve adjacent articles?

A2: These tags usually make intelligent judgments based on the following conditions of the current article:

1.  **发布时间:** 默认情况下,系统会按照文章的发布时间戳进行排序,找到在时间轴上紧邻当前文章的前一篇和后一篇。
2.  **内容模型和分类:** 它们倾向于在与当前文章相同的内容模型和分类下查找相邻文章,以保持内容的关联性。
这意味着,如果您需要调整上一篇/下一篇的顺序,调整文章的发布时间或在后台进行自定义排序是有效的方法。

Q3: Can I display other article information in the 'Previous'/'Next' navigation, such as thumbnails?

A3: Of course.prevandnextThe variable actually contains the complete article object of the previous/next article, which means you can access many of its properties. In additionTitleandLink, you can also getId(article ID),Keywords/Description(Article Summary),Thumb(Thumbnail) and other fields. For example, to display the thumbnail next to the previous article, you can modify the code like this:

”`twig {% prevArchive prev %} {% if prev %}

<span class="nav-

Related articles

How to use the `categoryList` tag to build and display a multi-level, nested category navigation menu?

In website operation, a clear and easy-to-navigate menu structure is a core factor in improving user experience and search engine friendliness.For websites with a large amount of content or a variety of product lines, it is particularly important to build a multi-level, nested category navigation menu.AnQiCMS provides powerful template tag functions, where the `categoryList` tag is the key to achieving this goal.This article will delve into how to use the `categoryList` tag to flexibly build and display your multi-level category navigation menu

2025-11-08

How to configure and display multilingual content in AnQi CMS to serve users in different regions?

In today's global internet environment, enterprises and content operators often need to provide content to users from different countries and regions.AnQiCMS (AnQiCMS) understands this need and therefore provides flexible and powerful multilingual content configuration and display capabilities, helping users easily expand into international markets and directly reach audiences with different language preferences.### Overview of AnQi CMS Multilingual Capabilities AnQi CMS considers multilingual support as one of its core strengths, aiming to provide users with a complete global content publishing solution

2025-11-08

How to implement lazy loading of images in article content to improve page loading speed and user experience?

In today's fast-paced online world, website loading speed is crucial for user experience and search engine rankings.If your website content contains a large number of images, optimizing their loading method will be a key step in improving website performance.Image lazy loading (Lazy Load) is a highly efficient solution. Why Image Lazy Loading is So Important? While website images can enrich content and enhance visual appeal, they are also often the main reasons for slow page loading.When a page carries many high-resolution images

2025-11-08

How to call and display product custom parameters (such as price, stock) on the product detail page?

In website operation, especially for product display websites, product details are far more than just titles and descriptions.In order to better meet user needs, it is particularly important to display various personalized parameters such as product price, inventory, color, material, and so on.AnQiCMS (AnQiCMS) takes advantage of its flexible content model and powerful template tag features, allowing you to easily call and display these custom parameters on product detail pages.### Preparations: Define Product Custom Parameters To display custom parameters on the product detail page, you first need to define these parameters in the AnQi CMS backend

2025-11-08

How to set up independent custom templates for specific articles, categories, or single pages to achieve personalized content display?

In content operations, creating unique visual and functional experiences for different parts of the website is crucial for enhancing brand image, optimizing user experience, and accurately conveying information.AnQiCMS (AnQiCMS) fully understands this, providing flexible template customization capabilities, allowing us to set personalized display templates for specific articles, categories, even independent single pages, thus achieving differentiated content presentation.The core of this feature lies in its ability to allow us to摆脱网站全局模板的限制, to design and apply specific layouts or styles targetedly, in order to better match content themes or operational objectives. For example

2025-11-08

Does AnQi CMS support the rendering of content in Markdown format and can it correctly display mathematical formulas and flowcharts?

For Anqi CMS users, whether they can use Markdown format for content creation and smoothly display complex mathematical formulas and visualized flowcharts is a key consideration for enhancing content expression.It is pleasing to see that Anqi CMS indeed provides good support for these advanced content formats, and through a set of flexible configuration mechanisms, allows content creators to easily achieve this goal. ### Enable Markdown Editor First, content creators need to make simple settings in the Anqi CMS backend.Enter the "Global Settings" menu

2025-11-08

How to dynamically display a list of friend links in the footer or sidebar of a website?

In website operation, friendship links are one of the important ways to enhance website authority, obtain external traffic, and improve user experience.A system that is easy to manage and can dynamically display friend links will undoubtedly greatly improve operational efficiency.AnQi CMS is an efficient content management system that provides flexible friend link management features, allowing you to easily display these valuable external resources in the footer or sidebar of your website.Friend link management in AnQi CMS is very intuitive.You only need to log in to the backend management interface of AnQi CMS

2025-11-08

How to automatically generate and display breadcrumb navigation that conforms to SEO standards on a webpage?

In website operation, providing clear navigation paths for visitors while optimizing the crawling efficiency of search engines is a key link to improve user experience and website SEO performance.A breadcrumb navigation is a feature that can intuitively display the hierarchical position of the user on the current page and provide a convenient return path.For websites built using AnQiCMS, we can conveniently utilize its built-in features to automatically generate and display breadcrumb navigation that complies with SEO standards.###

2025-11-08