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

Calendar 👁️ 64

When browsing website content, users often want to be able to navigate easily between related articles.For example, after reading a news article or tutorial, you can quickly click to go to the 'previous' or 'next' article to maintain the continuity of reading.This not only improves the user experience and reduces the bounce rate, but also has a positive effect on the internal link structure of the website and search engine optimization (SEO).

AnQiCMS (AnQiCMS) is an efficient content management system that provides a very simple and intuitive way to achieve this function.We do not need complex backend programming, just by reasonably using the built-in tags in the front-end template, we can easily realize the display of the previous and next article links on the article detail page.

Preparation: Understand the template structure

In AnQi CMS, the display of article detail pages is usually controlled by a specific template file. According to the AnQi CMS template design conventions, the template file for the document detail page is generally located in your template directory, for exampletemplate/您的模板目录/{模型table}/detail.htmlFor example, if it is an article model, it may betemplate/default/article/detail.html.

The Anqi CMS template engine syntax is similar to Django templates, variables are enclosed in double curly braces{{变量}}Define, while conditional judgment and loop control tags use single curly braces and percent signs{% 标签 %}Definition, and it must have an end tag. Understanding this will help us understand the subsequent code snippets.

Core Function: Tags for the previous and next article

The AnQi CMS is built-in with special tags to retrieve the previous and next articles, respectivelyprevArchiveandnextArchive. These tags are very smart, they automatically judge the context of the current article and obtain adjacent articles that are in the same category as the current article.

These tags are used in a similar way, both are block tags and require配合{% endprevArchive %}and{% endnextArchive %}Use the end tag. Inside the tag, we can define a variable to receive the article data obtained.

For example, the basic structure of displaying the previous article is as follows:

{% prevArchive prev %}
    {% if prev %}
        <a href="{{ prev.Link }}">{{ prev.Title }}</a>
    {% else %}
        <span>没有上一篇文章了</span>
    {% endif %}
{% endprevArchive %}

here,prevArchiveThe tag assigns the data of the previous article toprevthe variable. We use{% if prev %}To determine if there is a previous article. If it exists, display its link{{ prev.Link }}and the title{{ prev.Title }}If not found, display a prompt message, such as 'No previous article.'.

The display logic for the next article is also the same:.

{% nextArchive next %}
    {% if next %}
        <a href="{{ next.Link }}">{{ next.Title }}</a>
    {% else %}
        <span>没有下一篇文章了</span>
    {% endif %}
{% endnextArchive %}

Optimize display: More link display

It may be too simple to just display the title and link, in order to provide richer visual and information cues, we can add more elements such as thumbnails and descriptions to the links of the previous and next articles.

prevandnextVariables are article objects, they contain various field information of the articles, including commonly used ones:

  • Id: Article ID
  • Title: Article title
  • Link: Article link
  • Thumb: Article thumbnail URL
  • Description: Article description

We can flexibly use these fields to beautify the display of links. Here is a more detailed code example showing how to add thumbnails and truncated descriptions.

<div class="article-navigation">
    <div class="prev-article">
        {% prevArchive prev %}
            {% if prev %}
                <a href="{{ prev.Link }}" title="上一篇: {{ prev.Title }}">
                    {% if prev.Thumb %}
                        <img src="{{ prev.Thumb }}" alt="{{ prev.Title }}" class="prev-next-thumbnail"/>
                    {% endif %}
                    <p class="nav-label">上一篇</p>
                    <p class="nav-title">{{ prev.Title }}</p>
                    {% if prev.Description %}
                        <small>{{ prev.Description|truncatechars:50 }}</small> {# 使用truncatechars过滤器截取前50个字符 #}
                    {% endif %}
                </a>
            {% else %}
                <span class="no-article">没有上一篇文章了</span>
            {% endif %}
        {% endprevArchive %}
    </div>

    <div class="next-article">
        {% nextArchive next %}
            {% if next %}
                <a href="{{ next.Link }}" title="下一篇: {{ next.Title }}">
                    {% if next.Thumb %}
                        <img src="{{ next.Thumb }}" alt="{{ next.Title }}" class="prev-next-thumbnail"/>
                    {% endif %}
                    <p class="nav-label">下一篇</p>
                    <p class="nav-title">{{ next.Title }}</p>
                    {% if next.Description %}
                        <small>{{ next.Description|truncatechars:50 }}</small>
                    {% endif %}
                </a>
            {% else %}
                <span class="no-article">没有下一篇文章了</span>
            {% endif %}
        {% endnextArchive %}
    </div>
</div>

In this example, we usedivelements to wrap the links to the previous and next articles, and assignprev-articleandnext-articlesuch class names for convenient CSS styling later. We also addednav-labelandnav-titleUse to distinguish prompt text and article titles, and take advantage oftruncatechars:50The filter will truncate the description to 50 characters to avoid overly long text affecting the layout

Add the code to the template file

  1. Find the detail page template:Locate the template theme under which you are using the article detail page template file. For example, if it is a default theme article detail, the path may be similar totemplate/default/article/detail.html.
  2. Select the insertion position:The navigation to the previous and next articles is usually placed below the main content of the article, above the comment area, or in the sidebar of the article. Find{{archive.Content|safe}}(If usingarchiveDetailLabel, possibly{{articleContent|safe}}After that, and possibly before any comments section.
  3. Paste the code:Copy and paste the optimized code snippet into the selected location.
  4. Save and test:Save the template file and then visit any article detail page on the website to check if the links to the previous and next articles are displayed normally.
  5. Add CSS style (optional but recommended):According to the overall style of your website, addarticle-navigation/prev-article/next-article/prev-next-thumbnail/nav-label/nav-title/no-articleCSS styles to such classes, making them look more beautiful and integrated with the website.

By following these steps, you can elegantly display the links to the previous and next articles on the article detail page of Anqi CMS.

Frequently Asked Questions (FAQ)

  1. Why did I add tags but the link to the previous/next article did not display?
    • First, please check if your template syntax is correct, including tag names and variable names

Related articles

How to display mathematical formulas and flowcharts with Markdown in AnQiCMS?

In content operations, sometimes we need to display complex mathematical formulas or clear flowcharts, especially in technical articles, data reports, or educational content.AnQiCMS with its powerful Markdown editor, provides us with the convenience to meet this requirement.By simple configuration and importing the necessary library files, you can perfectly present these professional contents on the website front-end.### Enable AnQiCMS Markdown Editor Firstly, let AnQiCMS recognize and handle Markdown formatted content

2025-11-08

How to display the complete article content on the article detail page, including lazy-loaded images?

How to elegantly display full content and lazy loading images on article detail pages in AnQi CMS In website content operations, the article detail page is an important touchpoint for users to obtain information and gain a deeper understanding of brands and products.A well-designed, fast-loading, and comprehensive article detail page that can greatly enhance user experience and effectively assist in SEO optimization.For users who use Anqi CMS, taking full advantage of its powerful template tag system can easily realize the complete presentation of article content, and combine modern web technologies to implement lazy loading of images, making your website both beautiful and efficient.###

2025-11-08

How to display a document list based on different conditions (such as categories, recommended properties)?

In Anqi CMS, flexibly displaying website content is the key to improving user experience and meeting operational needs.Whether based on the category of the article or the recommended attributes of the content, the system provides powerful and easy-to-use template tags that allow you to easily customize the display of the document list. ### Core Tool: `archiveList` Document List Tag The core tool used in AnQi CMS to control the display of document lists is the `archiveList` tag.It allows you to filter, sort, and limit the number of displayed documents based on multiple conditions

2025-11-08

How to get and display all the content and associated images of a specific single page?

In Anqi CMS, a single page is an important part of the website content structure, such as "About Us", "Contact Information", and so on.Effectively acquire and display all the content of these single pages and associated images, which is crucial for the communication and visual expression of the website.Aanqi CMS provides an intuitive backend management and flexible template tags, making this process very convenient. ### Easily set up single page content and images in the background First, we need to create and edit single page content in the Anqi CMS backend management system. This is usually performed under the 'Page Resources' section in the 'Page Management' module

2025-11-08

How to display a list of related articles below the article detail page?

In website operation, the list of "related articles" at the bottom of the article detail page is an important function to enhance user experience, increase page views (PV), and prolong the time users stay on the site.It can guide users to discover more interesting content, thereby enhancing the overall stickiness of the website, and also has a positive effect on search engine optimization (SEO).To implement this feature in AnQi CMS, it benefits from its flexible template system and powerful tag functions, making the entire process intuitive and efficient.### One, understand the call mechanism of "related articles" The template tags of AnQi CMS are the core of realizing various functions

2025-11-08

How to display custom parameters on an article or product detail page?

In website operation, adding personalized information to articles or product detail pages is the key to improving content professionalism and user experience.AnQiCMS (AnQiCMS) fully understands this need and provides powerful customizable parameter functions, allowing us to flexibly add and display the required information according to different content types. Imagine you publish a technical article, in addition to the title and content, you also want to display unique attributes such as "author", "publish date", "technology stack", and so on; Or maybe, you are selling a product, in addition to the usual name, price, and description, you also want to highlight the "color"

2025-11-08

How to implement filtering conditions based on document parameters and display the filtering results?

In AnQiCMS (AnQiCMS), using document parameters to build flexible filtering conditions and display the filtering results is a key factor in improving website user experience and content management efficiency.This not only helps users find the information they need faster, but also makes the website's content structure clearer.This article will introduce you to how to implement this feature in AnQi CMS, from the initial parameter definition to the final filtering and display, helping you easily master content management.## Make content smarter: How to cleverly use document parameters to implement filtering function in Anqi CMS As the content of the website continues to enrich

2025-11-08

How to display a list of commonly used tags on a website or tags of specific documents?

In AnQiCMS, efficiently manage and display website tags, which can not only help users find the content they are interested in faster, but also effectively improve the SEO performance of the website.Tags as a flexible content classification method can associate content spanning different categories and even different models, forming a content matrix.Below, we will start from several common application scenarios and explain in detail how to display the list of commonly used tags on your website, as well as how to display associated tags for specific documents.### The Importance of Tags Tags are a powerful content management tool in AnQiCMS

2025-11-08