As an experienced website operations expert, I am well aware of the importance of breadcrumb navigation in enhancing user experience and optimizing search engine rankings.AnQiCMS (AnQiCMS) provides strong content operation support with its flexible template engine and rich tag system.However, when the content hierarchy of a website is deep, the breadcrumb navigation path becomes long, and how to elegantly display it has become a question worth discussing.
The template creation of Anqi CMS follows the Django template engine syntax, which means we have great freedom to control the presentation of content. Especially its built-inbreadcrumbThe label is the core of handling breadcrumb navigation. Next, we will delve into some effective optimization methods for displaying the AnQiCMS breadcrumb navigation path when it is too long.
Understand the breadcrumb navigation in AnQi CMS
In AnQi CMS, breadcrumb navigation is achieved by{% breadcrumb %}The label generates. This label will return an array object containing all the levels of the path from the current page to the homepage (usually namedcrumbs), each array element containsName(link name) andLink(Link address) and other information. This means that we are not just displaying a fixed path, but we can also refine the control of each level by traversing thiscrumbsarray, and refine the control of each level.
breadcrumbThe tag itself provides some parameters to adjust the default behavior, for exampleindexParameters can customize the displayed text on the homepage,titleThe parameter can control the display of the last (current page) breadcrumb item. This built-in flexibility lays the foundation for our optimization work.
Optimization strategy for the breadcrumb navigation path being too long
When the breadcrumb navigation path becomes too long, we can mainly optimize it from the following aspects:
1. Simplify the displayed text
A long breadcrumb path is often due to the long names of some levels. We can optimize the text from the following perspectives:
Review content naming specification:In the Anqi CMS backend, we can set 'category name', 'document title', and even 'custom URL' for categories, documents, single pages, and more.When planning the content structure, it should be made as concise and clear as possible.For example, 'Company news dynamics' can be simplified to 'Company news'.
Use the template filter to truncate text:If the name set in the background cannot be shortened further, we can use the string filter provided by Anqi CMS at the template level to truncate visually. For example,
truncatecharsThe filter can truncate long text and add an ellipsis at the end. We can do it like this:{% for item in crumbs %} <li><a href="{{item.Link}}">{{item.Name|truncatechars:15}}</a></li> {% endfor %}This code will truncate each breadcrumb item name to 15 characters, with an ellipsis for the remainder to avoid taking up too much space.
Use abbreviations or keywords:For some levels that users are very familiar with, it is considered to use abbreviations or more representative keywords to replace the full name.This usually requires combining front-end logic or customizing fields in the AnQi CMS backend, for example, adding a 'breadcrumb abbreviation' field for each category in the backend, and then displaying this abbreviation first in the template.
2. Smart Control Display Level
In some cases, even if the name is already concise, too many levels can make the navigation bar appear crowded. In this case, we can consider intelligently hiding or collapsing some of the middle levels.
Only show key levels:A common practice is to only display a few key nodes such as 'Home', 'Top Categories', 'Current Page', etc., while hiding or replacing the intermediate subcategories with '...'. In the Anqi CMS template, we can judge through
crumbsArray element index or reverse index (forloop.Counterandforloop.Revcounter) to implement this logic:{%- breadcrumb crumbs %} <nav aria-label="breadcrumb"> <ol class="breadcrumb"> {%- for item in crumbs %} {%- if forloop.First %} {# 始终显示第一个元素(通常是首页) #} <li class="breadcrumb-item"><a href="{{item.Link}}">{{item.Name}}</a></li> {%- elif forloop.Last %} {# 始终显示最后一个元素(当前页面) #} <li class="breadcrumb-item active" aria-current="page">{{item.Name}}</li> {%- elif forloop.Counter <= 2 or forloop.Revcounter <= 1 %} {# 显示前两个和倒数第二个元素 #} <li class="breadcrumb-item"><a href="{{item.Link}}">{{item.Name}}</a></li> {%- elif forloop.Counter == 3 and forloop.Revcounter > 1 %} {# 在中间插入省略号 #} <li class="breadcrumb-item ellipsis">...</li> {%- endif %} {%- endfor %} </ol> </nav> {%- endbreadcrumb %}This code will display the home page, the current page, and the first two and the second-to-last elements in the path, with an ellipsis to represent the excessive levels in between.
Collapsible navigation:For very deep content structures, consider collapsing the middle levels into a clickable "More" or "..." button, which, when clicked, expands to show the full path. This usually requires combining JavaScript and CSS to implement, and it is still traversed in the template.
crumbsAn array, but renders an interactive element under certain conditions.
3. Dynamic adaptation and responsive processing
The screen space of mobile devices is limited, and long breadcrumb navigation can seriously affect the user experience on mobile devices. Therefore, it is crucial to optimize for responsive design across different devices.
- CSS Media Queries:The most basic method is to use CSS media queries to adjust the breadcrumb style on small screen devices.For example, you can hide some less important levels or stack all levels and limit their width and font size.
- JavaScript Dynamic Adjustment:A more advanced strategy is to use JavaScript.On mobile, the breadcrumb navigation can be simplified to a mode that only displays 'Home' and the current page, with the middle path accessible through a clickable button (such as 'Go back a level' or 'View full path').The flexibility of AnQi CMS template allows us