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

Calendar Eye 129

It is crucial to provide a smooth user experience in the operation of our website.When visitors finish reading a richly contented article, they often hope to follow the trail and continue browsing other content of the same category or related topics.In this case, it is particularly important to display the titles and links of the 'previous article' and 'next article' on the article detail page.This not only significantly improves the user's reading experience and reduces the likelihood of them leaving the website, but also effectively guides users to deeply browse within the website, indirectly helping to improve the website's SEO performance.

AnQiCMS (AnQiCMS) has fully considered these actual needs of content operation when designed, providing a set of simple and efficient template tags that allow you to easily navigate to the previous and next articles on the article detail page without writing complex code.

Understand the key template tags

In AnQi CMS, the implementation of this function mainly depends on two built-in template tags:prevArchive(Previous article) andnextArchive(Next article).

  • prevArchiveTags:Used to get the data of the previous article of the current article.
  • nextArchiveTags:Used to get the data of the next article of the current article.

These tags are designed to automatically identify the current article and intelligently find the previous and next articles in the same category based on their publication time.They are context-sensitive, which means you just need to place them in the template of the article detail page, and the system will automatically handle the search logic.

How to useprevArchiveandnextArchive

The use of these tags is very intuitive, following the syntax rules of the Django template engine. You usually find them in the template files of the article detail page (such asarticle/detail.htmlorproduct/detail.htmlAdd them depending on your content model configuration.

Let's look at a simple example to see how they work.

Basic structure:

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

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

Code analysis:

  1. {%- prevArchive prev %}and{%- nextArchive next %}This is the start and end of the tag,prevandnextIt is the temporary variable name you define for the data of the previous and next articles.-symbols such as{%-and-%}This is used to remove the empty lines that may be generated by the template parsing itself, making the generated HTML cleaner.
  2. {%- if prev %}and{%- if next %}: This is a conditional judgment.prevArchiveandnextArchiveThe label will return an empty value when there is no corresponding previous or next article. By thisifJudgment, you can elegantly handle this situation, choose to display a prompt such as 'There is no previous article' or completely hide the navigation item.
  3. {{ prev.Link }}and{{ next.Link }}:prevandnextThe variable contains the complete information of the article.LinkField to retrieve the full link address of the article, which can be used directly.<a>label'shrefProperty.
  4. {{ prev.Title }}and{{ next.Title }}:TitleField to retrieve the title of the article, which can be displayed directly to the user.
  5. title="{{ prev.Title }}"andtitle="{{ next.Title }}": Add to the linktitleAttributes are a good practice that can display article titles when the user hovers, enhancing user experience and accessibility, and also providing some help to SEO.

More available article fields

exceptTitleandLink,prevandnextVariables also provide many useful fields that you can call as needed, for example:

  • Id: The unique ID of the article.
  • Thumb: The thumbnail address of the article, which will be very useful if you want your navigation to include images.
  • Description: The introduction of the article.
  • CreatedTime: The publication time of the article (timestamp format, you need to usestampToDateFormat, for example{{ stampToDate(prev.CreatedTime, "2006-01-02") }})
  • Views: Page views of the article.

For example, if you want to display thumbnails in navigation at the same time, you can modify it like this:

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

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

Remember that the above code only provides the HTML structure, and you can give it a beautiful style according to the overall style of your website, making it perfectly integrate with your website design.

ByprevArchiveandnextArchiveThese simple yet powerful tags make the navigation function of the article detail page accessible.It not only optimizes the user experience, but also indirectly helps the in-depth reading of website content and SEO performance.


Frequently Asked Questions (FAQ)

Ask: Why doesn't the link to my previous/next article display?

Answer: There may be several reasons. First, please confirm that the current article indeed has a previous or next article (for example, it is the first or last article in a category, or there is only one article in the category).Next, check if the template code uses{% if prev %}or{% if next %}This condition judgment, if the condition is not met, the navigation section may be hidden. In addition, make sure that your article has been published and the correct category has been set.

Question:prevArchiveandnextArchiveDoes the tag support specifying a model ID or category ID to get the previous/next article?

Answer:prevArchiveandnextArchiveTags are context-sensitive. They automatically identify the model and category of the current article and search for the previous/next article within the scope of that model and category.Therefore, they do not support additional inputmoduleIdorcategoryIdSpecify the parameters. This design simplifies usage and ensures the logic of navigation.

Ask: Can I display other information in the link of the previous/next article, such as the publication date or view count?

Answer: Absolutely.prevArchiveandnextArchivewithin the tag,prevornextThe variable is a complete article object, you can access all its available fields as you would on an article detail page. For example, you can display{{ prev.CreatedTime }}(Publish time) or{{ next.Views }}(Page views). It should be noted that,CreatedTimeis a timestamp, you need to use the formatting function provided by Anqi CMS to format it, for example,stampToDatefor example,{{ stampToDate(prev.CreatedTime, "2006-01-02") }}.

Related articles

How to implement the pagination display function of AnQiCMS article list, as well as how to customize the pagination style and logic?

In content operation, the pagination display of the article list is a basic and crucial function, which not only affects the user's browsing experience, but also is closely related to the performance and SEO performance of the website.AnQiCMS as an efficient and customizable content management system provides us with a flexible way to implement and beautify the pagination function of the article list.

2025-11-07

How to effectively retrieve and display a thumbnail, cover main image, or a collection of multiple images for an article?

In content operation, captivating images are an indispensable element to attract readers and enhance the quality of articles.AnQiCMS (AnQiCMS) understands this and provides a diverse and flexible set of features, whether it's for the thumbnails of article lists, the cover images on detailed pages, or rich multi-image collections, all can be easily obtained and displayed in the way you expect.How does AnQi CMS handle various images?In AnQi CMS, images are usually divided into several types to meet the display needs of different scenarios: * **Article Thumbnail (Thumb):**

2025-11-07

How to retrieve and display the title, detailed content, summary, and recommended attributes of an article (document)?

In Anqi CMS, whether it is to display the detailed content of a single article or to preview multiple articles on the list page, flexibly obtaining and displaying the title, detailed content, introduction, and recommended attributes of the article is the foundation of website operation.The design of Anqi CMS makes this process intuitive and efficient, we mainly achieve it through template tags and variable calls.First, understand the template system of Anqi CMS is crucial.

2025-11-07

How does AnQiCMS support the switching of multiple templates, as well as how to specify an independent template for a specific page or content type?

## The flexible aspects of AnQiCMS: How to handle multi-template switching and personalized page design A flexible template mechanism is crucial for a content management system to maintain competitiveness and adapt to diverse operational needs.AnQiCMS is an efficient enterprise-level content management system that has demonstrated excellent capabilities in this regard. It not only supports multi-template switching at the whole-site level but also provides detailed features for specifying independent templates for specific pages or content types. This is undoubtedly a powerful tool for websites that need to carry out refined content operations. Firstly

2025-11-07

How to configure and display related article lists on the article detail page, and specify relevance rules (such as keywords or manual settings)?

How to make visitors easily find more related content after reading an excellent article on your website, continue to explore your website, which can not only significantly improve user experience, but also is a key strategy to optimize website SEO and reduce bounce rate.AnQiCMS (AnQiCMS) offers powerful and flexible features, allowing you to easily configure and display related article lists on the article detail page and specify relevance rules according to actual needs.Next, we will together learn how to achieve this goal in Anqi CMS.

2025-11-07

How to retrieve and display a list of articles under a specific category (or multiple categories) in AnQiCMS?

In AnQiCMS, flexibly displaying website content is the key to improving user experience and website functionality.No matter if you are running a corporate website, a personal blog, or a marketing site, you often encounter the need to display a list of the latest articles, popular products, or multiple related category content under a specific category on a particular page.AnQiCMS's powerful template tag system, especially the `archiveList` tag, can help you easily achieve this goal.

2025-11-07

How to dynamically retrieve and display the content of custom model fields for an article in a template?

In AnQi CMS, the flexibility of website content is one of the key advantages for our content operation and website layout.By customizing content model fields, you can add unique properties for different types of content (such as articles, products), which can better reflect your business needs.But how can we dynamically present these carefully defined custom field contents in the front-end template of the website?This article will discuss this topic in detail, helping you fully utilize the powerful functions of AnQiCMS.

2025-11-07

How to retrieve and display the list of Tag tags and their corresponding links for an article?

In a website with increasingly rich content, how to effectively organize and present information is crucial.The Tag label (usually referred to as keywords or tags) not only helps users find related content quickly, but is also an indispensable part of Search Engine Optimization (SEO).AnQi CMS understands this point, providing users with a powerful and flexible tagging function, allowing you to easily add tags to articles and display these tags and their corresponding links in multiple forms on the website front end.

2025-11-07