As an experienced website operations expert, I know that every detail in a content management system can affect the user's visit experience and the overall performance of the website.Especially on the document details page, users expect to be able to browse related content smoothly.When the previous document is missing due to various reasons (such as being deleted, archived, or being the first one), how to elegantly provide a fallback option instead of letting the user get into a dead end, which is exactly the problem that AnQiCMS's flexibility can solve.
AnQiCMS content operation: How to elegantly guide users back to the list or homepage when the 'Previous article' document is missing?
In modern website operations, user experience is the core.When the user is browsing the article detail page, they usually expect to have 'Previous' and 'Next' navigation to facilitate continuous reading.However, there are always some documents that are the first in a category, or the previous document has been removed.In this case, if the "Previous" link simply disappears or displays a non-functional blank area, it may confuse the user and even lead them to leave the website.AnQiCMS (AnQiCMS) provides a clever solution to this problem with its flexible template tag system.
Our goal is to, when the previous document does not exist, no longer allow the link to hang in the air, but instead intelligently display a link to 'Return to List' or 'Return to Homepage', thus enhancing the user's browsing continuity.
Understand the working principle of the "Previous" tag
AnQiCMS's template engine is very powerful and intuitive, it providesprevArchiveLabel to get the previous content of the current document. The usage of this label is{% prevArchive prev %}...{% endprevArchive %}. When you call it in the template, if there is a previous document,prevThe variable will contain the details of the document, such as the title, link, etc. However, the key point is,When the previous document is not found,prevThe variable will be an empty value (in AnQiCMS's Go template engine, it can beifstatements are judged asfalse). It is this feature that provides the foundation for our implementation of smart fallback.
核心策略:条件判断与默认回退链接
要实现智能回退,我们只需利用AnQiCMS模板引擎的条件判断能力。当prevVariables have a value, and we normally display the 'Previous article' link; whenprevVariables are empty, we then enterelseBranch, at this point, you can provide a link back to the current category list based on actual operational needs, or more broadly, a link back to the homepage of the website.
Let's demonstrate how to elegantly implement this strategy through specific template code examples.
Option one: Return the current category list first, then the website homepage
we willprevArchiveThe label is wrapped in a conditional judgment. Inelsethe block, we first try to get the link of the current document's category. AnQiCMS providesarchiveDetailLabel to get the details of the current document, which includesCategoryfield, you can directly get the object and link of the current category.
The following is a template code snippet for this strategy:
<div class="pagination-section">
{% prevArchive prev %}
{% if prev %}
<div class="pagination-link prev-link">
<a href="{{ prev.Link }}" title="上一篇: {{ prev.Title }}">
« 上一篇: {{ prev.Title|truncatechars:30 }}
</a>
</div>
{% else %}
<div class="pagination-link default-link">
{# 尝试获取当前文档所属分类的链接 #}
{% archiveDetail currentCategory with name="Category" %}
{% if currentCategory.Link %}
<a href="{{ currentCategory.Link }}" title="返回{{ currentCategory.Title }}列表">
« 返回{{ currentCategory.Title }}列表
</a>
{% else %}
{# 如果无法获取分类链接,则回退到网站首页 #}
<a href="{% system with name='BaseUrl' %}" title="返回网站首页">
« 返回网站首页
</a>
{% endif %}
</div>
{% endif %}
{% endprevArchive %}
</div>
Code analysis:
{% prevArchive prev %}:Try to get information of the previous document.{% if prev %}IfprevIf the variable has a value (i.e., there is a previous document), then display the normal link. Here, it usesprev.Title|truncatechars:30to truncate the title and keep the page tidy.{% else %}IfprevThe variable is empty (no previous document), entering fallback logic.{% archiveDetail currentCategory with name="Category" %}In the context of the current document, obtain its category object and assign it tocurrentCategorya variable.{% if currentCategory.Link %}:Check if the category link is successfully obtained. If obtained, display the link 'Return [category name] list'.currentCategory.LinkandcurrentCategory.TitleDirectly provides the category URL and name.{% else %}If it is not possible to obtain the classification link (for example, the document may not have associated categories, or there is an exception in the data), then execute the final fallback and display the link to 'Return to the website homepage'.{% system with name='BaseUrl' %}: AnQiCMS'ssystemLabels can conveniently obtain the global configuration of the website, including the URL of the home page.
Through such layered judgments, we ensure that users can always obtain a valid navigation direction, greatly enhancing the user experience. In practical applications, you can tailor the UI/UX design of the website to.pagination-link/.prev-linkand.default-linkAdd the corresponding CSS style to make it visually more harmonious and beautiful.
Option two: directly return to the website homepage (simpler, suitable for all situations)
If you prefer a simple and direct fallback solution, which is to return to the homepage in any case where there is no previous one, the code can be more concise:
`twig
{% prevArchive prev %}
{% if prev %}
<