How to display 'Previous article' and 'Next article' links on the article detail page?

Calendar 👁️ 62

In website content operation, optimizing the user's reading experience has always been one of our core goals.When immersed in an excellent article, it is natural to want to smoothly switch to a related or next article without having to return to the list page to search for it.This seamless navigation not only effectively improves the user's stay time on the website and reduces the bounce rate, but also helps search engines better understand the structure of the website content, thereby having a positive impact on SEO.

AnQi CMS provides a very intuitive and easy-to-use solution that allows you to easily add "Previous" and "Next" article links on the article detail page.The entire process does not require complex configuration, just add a few lines of simple code to the template file and it can be achieved.

The core of implementing 'Previous' and 'Next' links

The Anqi CMS template engine supports tag syntax similar to Django, it provides two tags specifically for retrieving adjacent article content:prevArchiveandnextArchive. These tags are powerful because they can intelligently identify and return the previous or next article data under the same category based on the context of the current article, without the need to manually pass the article ID or category ID.

Let's first take a look at their basic usage, you can try adding the following code in the template file of the article detail page (usually){模型table}/detail.html)

<div class="article-pagination">
    <div class="prev-article">
        {% prevArchive prev %}
        {% if prev %}
            <a href="{{ prev.Link }}">上一篇:{{ prev.Title }}</a>
        {% else %}
            <span>没有上一篇了</span>
        {% endif %}
        {% endprevArchive %}
    </div>

    <div class="next-article">
        {% nextArchive next %}
        {% if next %}
            <a href="{{ next.Link }}">下一篇:{{ next.Title }}</a>
        {% else %}
            <span>没有下一篇了</span>
        {% endif %}
        {% endnextArchive %}
    </div>
</div>

In this code,{% prevArchive prev %}Attempts to get the previous article of the current article and assign all its data toprevthe variable. Similarly,{% nextArchive next %}will assign the data of the next article tonextthe variable. We use{% if prev %}and{% if next %}To determine if an article exists, if it exists, display a link containing the article ({{ prev.Link }}/{{ next.Link }}) And Title ({{ prev.Title }}/{{ next.Title }}The link if it exists; if not, it displays the prompt 'No previous article' or 'No next article'.

Display rich links to enhance user experience.

It may not be enough to just display the title, to make the navigation links more intuitive and attractive, we can useprevandnextVariables contain more article information, such as thumbnails. These variables are actually complete article objects, you can use them to.An operator accesses its various fields, like accessing the details of the current article (archive) in the same way.

The following is a more comprehensive code example, which tries to display the thumbnails of the previous and next articles while showing the title and link:

<div class="article-navigation-section">
    <div class="prev-nav">
        {% prevArchive prev %}
        {% if prev %}
            <a href="{{ prev.Link }}" title="{{ prev.Title }}">
                {% if prev.Thumb %}
                    <img src="{{ prev.Thumb }}" alt="{{ prev.Title }}" class="nav-thumb-left">
                {% endif %}
                <div class="nav-text">
                    <span>上一篇</span>
                    <h3>{{ prev.Title }}</h3>
                </div>
            </a>
        {% else %}
            <div class="no-nav-item"><span>没有上一篇了</span></div>
        {% endif %}
        {% endprevArchive %}
    </div>

    <div class="next-nav">
        {% nextArchive next %}
        {% if next %}
            <a href="{{ next.Link }}" title="{{ next.Title }}">
                <div class="nav-text">
                    <span>下一篇</span>
                    <h3>{{ next.Title }}</h3>
                </div>
                {% if next.Thumb %}
                    <img src="{{ next.Thumb }}" alt="{{ next.Title }}" class="nav-thumb-right">
                {% endif %}
            </a>
        {% else %}
            <div class="no-nav-item"><span>没有下一篇了</span></div>
        {% endif %}
        {% endnextArchive %}
    </div>
</div>

In this example, we added different CSS classes to the thumbnails of the previous and next posts (nav-thumb-leftandnav-thumb-right), so that you can apply different layouts and styles with custom CSS, such as placing the thumbnail of 'Previous Post' on the left and the thumbnail of 'Next Post' on the right. Remember to define these styles in your theme CSS file to make them effective.article-navigation-section,.prev-nav,.next-nav,.nav-thumb-left,.nav-thumb-right,.nav-text,.no-nav-itemStyle of the same type.

Integrate the code into the article detail template

You just need to copy and paste the above code snippet into your article detail page template file (for example, located in/template/{您的模板目录}/article/detail.htmlor/template/{您的模板目录}/{您的自定义模型}/detail.html) Where you want to display the navigation link. Usually, these links are placed at the end of the article content or in a dedicated sidebar area.

ByprevArchiveandnextArchiveThese two concise and powerful tags make the website navigation more intelligent and user-friendly with Anqie CMS.This greatly enhances the user's reading experience, and also allows your website content to be discovered and consumed better.

Frequently Asked Questions (FAQ)

What is the logic for determining the 'Previous' and 'Next' articles? Can I customize it to show articles by the same author or tag?Of Security CMSprevArchiveandnextArchiveThe label is determined based on the publication time and category of the current article.They will search for the adjacent articles before and after the current article in the category it is in, arranged in chronological order.Currently, these tags do not directly support customization for complex judgment logic such as 'same author', 'same tag', or 'same series'.If you need to implement such advanced associations, you may need to combinearchiveListtags, throughtype="related"orlike="keywords|relation"Parameters, and may require additional template logic processing.

2. In addition to the title and link, what article information can I display in the 'Previous' and 'Next' links? prevandnextThe variable actually contains the complete data objects of the previous and next articles, and the fields accessed are the same as those on the current article detail page.{{ archive.字段名 }}Besides,Link(article link) andTitle(Article Title), you can also displayThumb(Thumbnail),Description(Article Summary),CreatedTime(Publish Time, you need to use}stampToDateFormat) etc. You can flexibly choose these fields to enrich the display of navigation links.

3. Will the page show an error if there is no previous or next article?No. As shown in the above code, we used{% if prev %}and{% if next %}Such conditional judgment. If the current article is the first in the category,prevthe variable will be empty; if it is the last article,nextThe variable will be empty. By making such a judgment, you can elegantly display the prompt text such as 'No previous article' or 'No next article' without causing a page error, ensuring the stability and user experience of the website.

Related articles

How to sort the list display according to the creation time or view count of the articles?

In website content operation, the presentation sequence of content is crucial for user experience and information delivery efficiency.Whether it is a news portal, technology blog, or product showcase website, reasonably adjusting the sorting method of the content list according to different operational goals can significantly enhance the vitality and attractiveness of the website.AnQiCMS (AnQiCMS) is well-versed in this, providing intuitive and powerful features that allow us to easily sort the content list by the creation time or number of views.###

2025-11-08

How to implement the pagination display function of the article list in AnQiCMS?

The pagination feature of the article list is crucial for any content management system, as it not only improves the user browsing experience and avoids the slow loading caused by overly long single pages, but also helps search engines better crawl and index website content.In AnQiCMS, implementing this feature is both flexible and efficient, thanks to its concise template tag design. Next, we will together learn how to easily implement pagination display of article lists in AnQiCMS.

2025-11-08

How to display the viewing volume of articles or products on the frontend page?

Bring content to life: easily display the number of views for articles or products on the Anqi CMS front-end page In website operations, the number of views for articles or products is often an important indicator of how popular the content is.It not only provides visitors with an intuitive reference, forming a "social identity" effect, guiding them to discover popular content, but also helps operators quickly identify high-value content.AnQi CMS is an efficient content management system that fully considers the actual needs of users, making it very simple and intuitive to display the number of views of articles or products on the front-end page.

2025-11-08

How to set a default thumbnail to automatically display when there is no image in the article?

In content operation, images play a crucial role.They can not only catch the visitor's eye, enhance reading interest, but also effectively convey information and enhance the brand image.However, when updating articles daily, we may occasionally forget to upload thumbnails, or some content is not suitable for pictures.This is when a blank page appears on the article list or detail page, which seems unprofessional and affects user experience.Fortunately, AnQiCMS (AnQiCMS) has considered this point and provided a very practical feature: setting a default thumbnail.With it, even if the article does not have a picture

2025-11-08

How to create and display multi-level navigation menus in AnQiCMS?

In website operation, a clear and effective navigation menu is the core of user experience, which can help visitors quickly find the information they need.AnQiCMS (AnQiCMS) knows this well and provides flexible and powerful features for you to easily create and manage multi-level navigation menus, whether it is a simple two-level dropdown menu or a complex menu combined with dynamic content display, it can meet your needs. Next, we will learn together how to create and display multi-level navigation menus in AnQiCMS.### One, Back-end Management

2025-11-08

How to highlight the link of the current visited page in the navigation menu?

When a user visits your website, a clear and intuitive navigation menu can significantly enhance their browsing experience.One important optimization is to make the navigation menu intelligently highlight the current page link being visited, so that visitors can clearly know where they are on the website.AnQi CMS provides a simple and powerful way to implement this feature, without complex development, just need to configure cleverly in the template.### Understanding the navigation mechanism of Anqi CMS Anqi CMS is built with a flexible template tag system, the core of which is the "navigation list tag"

2025-11-08

How to set and display the common areas of the website such as header, footer, and sidebar in AnQiCMS?

In website operation, the header, footer, and sidebar, etc., play a crucial role.They not only carry the brand image and navigation function, but also the key to improving user experience and content distribution efficiency.For those who use AnQiCMS, understanding how to efficiently set up and display these areas will greatly enhance the professionalism and operational convenience of the website.AnQiCMS as an enterprise-level content management system based on the Go language, has fully considered the flexibility and scalability of templates from the very beginning.This means, whether it is to build a simple corporate website

2025-11-08

How to correctly display the breadcrumb navigation path on a web page?

Elegantly configure breadcrumb navigation in AnQiCMS: Improve user experience and SEO performance When you visit a website, breadcrumb navigation is like a considerate guide, it can clearly tell you where the current page is in the website structure.From the home page all the way to the page you are browsing, it not only helps users quickly understand the website hierarchy, but also makes it convenient for them to backtrack to the upper page.For the website's search engine optimization (SEO), a reasonable breadcrumb navigation can also help search engines better understand the website structure, thereby possibly bringing better crawling and ranking effects

2025-11-08