How to customize the display logic and content sorting of the search result page of Anqi CMS?

Calendar 👁️ 69

Optimize the search results page of the website so that it can not only accurately present information but also intelligently sort according to user preferences, which is a key link to improving user experience and website efficiency.AnQi CMS provides a powerful and flexible template engine and a rich set of tags, making it intuitive and efficient to customize the display logic and content sorting of search result pages.

In AnQi CMS, the search results page is usually controlled by a specific template file, which is located by default in the directory of the template theme you are using.search/index.html(or in the case of flat mode'ssearch.html). To customize the display of search results, we will mainly focus on this template file and the built-in template tags of Anqi CMS.

Understand the search mechanism of Anqi CMS.

When a user performs a search on your website, they will usually visit a URL similar to/search?q=关键词. Anqi CMS will analyze the keywords in the URL (qSearch for relevant content and pass this content along with other page information tosearch/index.htmlto render the template.

during this process,archiveListTemplate tags are the key to obtaining core search results. They can filter and sort content based on various conditions and are the basis for customizing the display logic and sorting rules of the search results page.

Custom search results page display content

First, you need to find and editsearch/index.htmlThe template file. In this file, you will see the code used to loop display the search resultsarchiveListTags andforLoop.

A basic search results list may look like this:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div class="search-result-item">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>{{item.Description|truncatechars:150}}</p>
        <div class="meta-info">
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
            <span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{{item.Views}}</span>
        </div>
        {% if item.Thumb %}
        <a href="{{item.Link}}">
            <img alt="{{item.Title}}" src="{{item.Thumb}}">
        </a>
        {% endif %}
    </div>
    {% empty %}
    <p>抱歉,没有找到与“{{urlParams.q}}”相关的结果。</p>
    {% endfor %}
{% endarchiveList %}

In the code above:

  • {% archiveList archives with type="page" limit="10" %}: This line of code tells the system to get the document list and display it in the form of pagination (type="page"), showing 10 results per page (limit="10").archivesis a variable that contains all the document data that meets the conditions. On the search results page, it will automatically capture the URL inqsearch with the keywords.
  • {% for item in archives %}: Loop through.archiveseach document record.
  • {{item.Title}}/{{item.Link}}/{{item.Description}}Equal: These are document objects (item) properties, you can display the title, link, summary, thumbnail information of the document as needed.
  • |truncatechars:150This is a filter used to truncate the content of the profile description to no more than 150 characters, and automatically add an ellipsis at the end to keep the page neat.
  • {% categoryDetail with name="Title" id=item.CategoryId %}Through the document'sCategoryIdGet and display the name of its category.
  • {{stampToDate(item.CreatedTime, "2006-01-02")}}: This is a formatted timestamp label, converting the document creation time to a readable date format.
  • {% empty %}When there are no search results, this part of the content will be displayed to enhance user experience.
  • {{urlParams.q}}Directly obtain the current search keyword from the URL to prompt the user when there are no results.

In addition, you can display any custom fields of the document, just by specifying the field name in thearchiveDetailtag, or by looping through all custom parameters.archiveParamsto display all custom parameters.

Optimize the content sorting logic

The sorting method of search results directly affects the efficiency of users finding the information they need. Anqi CMS'sarchiveListtags provide flexibleorderparameters, allowing you to easily control the sorting of content.

By default, search results may be sorted in descending order by ID (newest first), but you can adjust according to your business needs. For example, if you want to sort by views in descending order, you canarchiveListthe tag withorder="views desc":

{% archiveList archives with type="page" limit="10" order="views desc" %}
    {# ... 循环显示搜索结果 ... #}
{% endarchiveList %}

Common sorting methods include:

  • id desc: Sorted in descending order by document ID (usually the most recent release is prioritized).
  • id asc: Sorted in ascending order by document ID (usually the oldest release time is prioritized).
  • views desc:By view count in descending order (popular content first).
  • views asc:By view count in ascending order.
  • sort desc:By custom sorting field in descending order.

Further, you can also provide users with sorting options. This is usually achieved by adding a dropdown menu or button group to the page.When the user selects a different sorting method, the page will reload and a sorting parameter will be added to the search URL (for example&order=views_desc)。Then,archiveListIn the tag, you can dynamically read this URL parameter to setorder.

{# 假设您在URL中通过参数 'sort_by' 传递排序方式 #}
{% archiveList archives with type="page" limit="10" order="{{urlParams.sort_by|default:'id desc'}}" %}
    {# ... 循环显示搜索结果 ... #}
{% endarchiveList %}

At this time, you need to provide a sorting selection UI on the page, for example:

<select onchange="window.location.href = updateQueryStringParameter(window.location.href, 'sort_by', this.value);">
    <option value="id desc" {% if urlParams.sort_by == 'id desc' or not urlParams.sort_by %}selected{% endif %}>最新发布</option>
    <option value="views desc" {% if urlParams.sort_by == 'views desc' %}selected{% endif %}>热门浏览</option>
</select>
<script>
function updateQueryStringParameter(uri, key, value) {
    var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
    var separator = uri.indexOf('?') !== -1 ? "&" : "?";
    if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
    } else {
        return uri + separator + key + "=" + value;
    }
}
</script>

After the user selects, the page will automatically refresh and display the results according to the new sorting rules.

Enhance the search results display logic: filtering and categorization

In addition to sorting, the user

Related articles

How to display user submitted content in the comment list or message board of Anqi CMS?

In website operation, user-generated content (UGC) is an important aspect for enhancing interaction and activity, whether it is the comments below the articles or the overall message board of the website, it can effectively promote user communication and feedback collection.AnQiCMS was designed with a strong emphasis on user interaction from the beginning and provides powerful and flexible features for managing and displaying the content submitted by users. In order to display user-submitted comments or messages on Anqi CMS, the key is to understand its template mechanism and the corresponding built-in tags.

2025-11-09

How to implement lazy loading of images and automatically convert them to WebP format to optimize display performance in AnQi CMS?

## Speed up website image loading: The secret of AnQiCMS image lazy loading and automatic WebP conversion In today's increasingly rich digital content, the quality and loading speed of website images directly affect user experience, search engine rankings, and the overall conversion rate of the website.Imagine if the images on a website load slowly, visitors are likely to leave before the content is fully displayed.The good news is that AnQiCMS has provided us with a set of efficient solutions, through the two functions of image lazy loading and automatic WebP conversion, so that the visual content of the website can be guaranteed in terms of quality at the same time

2025-11-09

How to safely render HTML content generated by a rich text editor in the Anqi CMS template?

When building a website, a rich text editor (Rich Text Editor, abbreviated as RTE) is undoubtedly a powerful tool for content creation. It allows operators to edit content in a visual way, easily adding formats, images, links, and even tables, greatly enhancing the richness of content presentation.However, when this HTML content generated by RTE needs to be displayed in the front-end template of the website, how to ensure its safe rendering while retaining the expected style and structure is a problem that requires careful consideration.

2025-11-09

How to configure and display the site name, Logo, and filing information for Anqi CMS website?

The identity of a website often starts with its site name, logo, and filing information.In AnqiCMS, configuring and displaying these core information is the first step in building a website and is also a crucial step.The entire process is intuitive and convenient, even if you are a beginner using AnqiCMS, you can easily complete it.Next, we will introduce how to complete these settings in AnqiCMS and display them on your website.--- ### **First Step: Configure the website's basic information** First

2025-11-09

How can Anqi CMS prevent the malicious collection of content and add watermarks to images for display?

Today, with the increasing importance of digital content, protecting original content from malicious collection and misuse has become a challenge for many website operators.Especially images, as the core carrier of visual information, the copyright protection should also not be neglected.AnQi CMS understands these pain points, therefore, in the system design, it especially integrates powerful anti-collection interference codes and image watermark functions, building a solid protective wall for our digital assets.### A clever layout to prevent malicious scraping Malicious scrapers often use automated programs to bulk harvest website content for unauthorized copying and distribution

2025-11-09

How to independently configure and display the content of each site in the Anqi CMS multi-site environment?

In today's rapidly developing digital age, many businesses and individual operators may not have just one website, but need to manage multiple brand sites, product display pages, or niche field blogs.How to ensure that in such a multi-site environment, each site's content can be independently configured, flexibly displayed, and maintain efficient management, which is a great challenge facing the operators.AnQiCMS (AnQiCMS) takes advantage of its powerful multi-site management function, providing an elegant and practical solution for this.The AnQi CMS was designed from the outset to consider the needs of multi-site operations

2025-11-09

How to customize TDK (Title, Description, Keywords) in Anqi CMS to optimize the display of pages in search results?

In website operation, the display effect of the page in search results is crucial for attracting users and increasing traffic.Among them, TDK (Title, Description, Keywords) is the core element that search engines understand the content of the page and decide how to display the page.AnQiCMS (AnQiCMS) provides users with flexible and powerful TDK customization features, helping us refine page optimization, thereby achieving better performance in search results.

2025-11-09

How to implement conditional judgment and loop traversal in the Anqi CMS template to control content display?

In Anqi CMS, templates are the core of website content presentation, they are not only responsible for the layout and style of content, but also bear the important responsibilities of displaying different content based on different conditions and cyclically presenting data lists.To make your website content more dynamic and interactive, it is particularly important to have a deep understanding of how to implement conditional judgments and loop traversals in templates.The Anqi CMS template engine uses syntax similar to Django, which allows you to quickly get started and flexibly apply it if you are familiar with web development.### Understand AnQiCMS

2025-11-09