How to obtain and display the links and titles of the previous and next articles?

Calendar 👁️ 114

When we manage and publish content in Anqi CMS, in order to enhance the user experience and content discoverability of the website, it is usually necessary to provide the function of navigating to the "previous" and "next" articles on the article detail page.This not only encourages readers to continue browsing the site's content, but also provides a good internal link structure for search engine optimization (SEO).

The AnQi CMS provides a very concise and intuitive template tag, allowing us to easily achieve this requirement.

Core function:prevArchiveandnextArchiveTag

AnQi CMS provides us with two special template tags for retrieving adjacent article data:prevArchiveUsed to get the previous article, andnextArchiveIt is used to get the next article. Their strength lies in the fact that there is no need to manually specify the article ID or category, the system will intelligently find the article adjacent in time sequence to the current article.

These tags are usually on the article detail page (for examplearticle/detail.htmlOr a custom model corresponding todetail.htmlIs used in the template to ensure that they can accurately identify the article in the current context.

Basic usage: Only display title and link

In your article detail template, you can use the following code snippet to retrieve and display the title and link of the previous and next articles.An important practice is to check if the article exists before using it to avoid empty links or errors when there are no adjacent articles.

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

In the code above:

  • {% prevArchive prev %}Defined a namedprevThe variable, it will carry all the data of the previous article.
  • {% nextArchive next %}Similarly, a variable namednextis defined to store the data of the next article.
  • {% if prev %}and{% if next %}Is a conditional statement to ensure that the link and title will be displayed only when the previous article or the next article exists.If not found, it will display a prompt like 'Not available'.
  • {{ prev.Link }}and{{ next.Link }}Retrieve the URL links of the previous and next articles.
  • {{ prev.Title }}and{{ next.Title }}Retrieve the titles of the previous and next articles.

In this way, you can clearly present article navigation at the bottom or sidebar of the article.

Advanced usage: combine thumbnails to enhance visual experience.

To make the navigation of the previous and next articles more prominent and attractive, we can choose to add thumbnails. The article data of Anqi CMS usually containsThumb(thumbnail) orLogo(Cover first image) field, you can choose to use it according to your design requirements.

Here is an example of how to integrate thumbnails in links:

<div class="article-navigation-visual">
    <div class="nav-item prev-item">
        {% prevArchive prev %}
        {% if prev %}
        <a href="{{ prev.Link }}">
            {% if prev.Thumb %}
            <img src="{{ prev.Thumb }}" alt="上一篇:{{ prev.Title }}" />
            {% endif %}
            <span>上一篇:{{ prev.Title }}</span>
        </a>
        {% else %}
        <span class="no-article">没有上一篇文章了</span>
        {% endif %}
        {% endprevArchive %}
    </div>
    <div class="nav-item next-item">
        {% nextArchive next %}
        {% if next %}
        <a href="{{ next.Link }}">
            {% if next.Thumb %}
            <img src="{{ next.Thumb }}" alt="下一篇:{{ next.Title }}" />
            {% endif %}
            <span>下一篇:{{ next.Title }}</span>
        </a>
        {% else %}
        <span class="no-article">没有下一篇文章了</span>
        {% endif %}
        {% endnextArchive %}
    </div>
</div>

In this advanced example, we added a{% if prev.Thumb %}The judgment ensures that the image is displayed only when the article has a thumbnail.altThe setting of the property is also very important for SEO and accessibility. You can also, as needed,prevandnextAccess other fields within a variable, for exampleDescription(Article summary),Views(Views) orCreatedTime(Publish time) etc.

Where should the code be placed?

These template codes are usually placed in your article detail page template. Depending on your template design, it may be:

  • Below the main content area of the article.
  • The top of the comment section or related article recommendation area.
  • The fixed position of the page sidebar.

In Anqi CMS, the template file of the article detail page is usually named.{模型table}/detail.html(For example)article/detail.htmlorproduct/detail.html). Just open the corresponding template file, paste the above code in the appropriate position, and then save it.

A温馨提示 with **practice

  • Customize the style:The code provided only offers the basic HTML structure. To make the navigation of the previous and next articles look coordinated and beautiful on your website, you need to write the corresponding CSS styles to beautify them.
  • Clear instructions: Ensure that the text descriptions of 'Previous' and 'Next' are clear, making it easy for users to understand.
  • Uniformity:Maintain the consistency of the navigation area design throughout the website, which helps to improve the overall user experience.
  • SEO friendly:This internal link structure naturally guides search engine crawlers to discover more pages, which helps improve the inclusion and ranking of website content.

By using these simple and practical template tags, you can easily build a highly efficient and user-friendly article navigation function in AnQi CMS, which will further enhance the value of your website.


Frequently Asked Questions (FAQ)

Q1: What will be displayed if my article does not have a previous or next article?A1: As shown in the example code in the article, we recommend that you use{% if prev %}or{% if next %}To determine whether the previous or next article exists.

Related articles

How to integrate a category list in the navigation menu and display product documents under each category?

In AnQi CMS, integrating product categories into the navigation menu and displaying the product documents below is a crucial step to enhance website user experience and content organization efficiency.This can help visitors quickly find the information they need, and it also helps search engines better understand the structure of the website. To achieve this goal, we need to make some basic settings in the Anqi CMS backend and integrate the corresponding code in the template files according to these settings.

2025-11-08

How to implement nested category display with multiple levels and how to judge if there is a subcategory in the template?

When building a website, a clear and organized content structure is crucial for enhancing user experience and SEO performance.AnQiCMS (AnQiCMS) relies on its flexible content model and powerful template engine, allowing us to easily implement complex multi-level nested display and subcategory judgment, thus creating highly customized navigation and content layouts. ### Backend Settings: Build Category Hierarchy In the Anqi CMS backend, the category management feature (usually located under the "Content Management" menu, below "Document Category") is the foundation for implementing multi-level categorization. First

2025-11-08

How to retrieve and display details of a specific category, including description, content, and Banner image?

When operating a website, we often need to display unique content for different category pages, such as a product category may have a detailed introduction and an eye-catching Banner image, while a news category may focus more on a concise description.AnQi CMS provides a flexible mechanism that allows us to easily retrieve and display detailed information about specific categories, including descriptions, content, and Banner images.### Backend settings are fundamental: enriching categories Before displaying category details on the frontend, we need to fill in this information in the Anqi CMS backend

2025-11-08

How to add and display a captcha in the comment or message form to prevent spam?

When operating a website, spam comments and malicious messages are always a headache, as they not only affect user experience but may also damage the professional image of the website.Fortunately, AnQi CMS provides a simple and effective way to solve this problem - that is to add a captcha to the comment or message form.This can greatly enhance the security of the website, filtering out most automated spam information.Below, let's take a detailed look at how to add a captcha to your comment and message form in Anqi CMS.This process can be divided into two steps, which is very intuitive and easy to operate.### Step 1

2025-11-08

How to retrieve and display the Logo, thumbnail, and slideshow group on a single page?

In Anqi CMS, single pages (such as "About Us", "Contact Us", etc.) can not only carry rich text information but also greatly enhance the attractiveness and professionalism of the page through visual elements.Retrieve and display the Logo, thumbnail, and slideshow group on a single page, which is a key step to optimize the page display effect.AnQi CMS provides flexible template tags, allowing you to easily achieve these functions.### Understand the composition of visual elements on a single page In Anqi CMS, each single page can have exclusive visual elements to meet different display needs

2025-11-08

How to display copyright information and link to the record number at the bottom of the website?

Clearly display copyright information and filing number at the bottom of your website, which is not only a legal requirement but also a key factor in enhancing the professionalism and user trust of the website.AnQi CMS as an efficient content management system, fully considers these details, allowing you to easily manage and present these important site information. ### Understanding the Importance of Copyright Information and Filing Numbers The copyright statement at the bottom of the website, like a brand watermark, clearly identifies the ownership of the content to visitors, effectively protecting your original achievements.As for websites operating in mainland China

2025-11-08

How to dynamically control the display of content in the template based on conditional logic (if/else)

In AnQiCMS template design, it is possible to flexibly control the display of content based on different conditions, which is the key to achieving website personalization and dynamic display.Whether it is to decide whether to display the picture according to whether the article has a thumbnail, or to display specific content based on the user's role, conditional logic can provide strong support.AnQiCMS uses a syntax similar to the Django template engine, which makes it very easy for webmasters familiar with web development to get started quickly.### Master the basic conditional logic in AnQiCMS templates AnQiCMS

2025-11-08

How to use for loop to traverse list data in the template and display it?

In website content management, dynamically displaying list data is a very basic and important function.Whether it is a news list, product display, category navigation, or friend links, we need to be able to flexibly obtain data from the backend and present it on the frontend template.AnQiCMS (AnQiCMS) provides a powerful and easy-to-use template engine that allows us to easily meet these requirements, among which the 'for loop' is the core tool for displaying list data.AnQiCMS's template engine adopts a syntax style similar to Django

2025-11-08