How to control the display sorting and pagination of AnQiCMS comment list?

Calendar 👁️ 69

In Anqi CMS, efficiently managing and displaying website comments is crucial for enhancing user interaction and optimizing content experience.The display order and pagination settings of the comment list directly affect the user's experience when browsing comments.This article will give you a detailed explanation of how to accurately control the sorting and pagination behavior of comments in AnQiCMS.

Understand the template structure of the comment list

Firstly, we need to clarify the position of the comment list in AnQiCMS. According to the system's design conventions, the comment list is usually located in the current template directory ofcomment/list.htmlRendering in the file.This file is the core of your custom settings.AnQiCMS uses a syntax similar to Django template engine, controlling data through specific tags.

Core tags:commentListand its parameters

To control the comment list, we mainly rely oncommentListThe tag is responsible for retrieving comment data from the database. It provides a series of parameters that allow you to flexibly define the way comments are retrieved.

when you arecomment/list.htmlWhen calling the comment list, you will usually see a structure like this:

{% commentList comments with archiveId=archive.Id type="page" limit="10" %}
    {# 评论循环体 #}
{% endcommentList %}

HerearchiveId=archive.IdIt represents getting the comments of the current article. Next, we will focus ontype/limitandorderthese key parameters.

Control the display order of comments

The display order of comments is determined byorderThe parameter. You can set different values to determine the sorting method of comments.

  • Sort by the most recent post (default)If you want the latest comments to appear at the top, you can useorder="id desc". This is a common sorting method to ensure that users can see the latest discussions.
  • Sort by earliest published: If you want the comments to be arranged in chronological order, with the earliest comments first, you can useorder="id asc".

For example, if you want comments to be sorted in reverse chronological order (with the most recent comments first), you cancommentListset the tag as follows:

{% commentList comments with archiveId=archive.Id type="page" limit="10" order="id desc" %}
    {# 循环显示评论内容 #}
    {% for item in comments %}
        <div>
            <!-- 显示评论用户名、时间、内容等 -->
            <span>{{item.UserName}}</span>
            <span>{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
            <p>{{item.Content}}</p>
            {% if item.Parent %}
                <blockquote>回复:{{item.Parent.UserName}} - {{item.Parent.Content}}</blockquote>
            {% endif %}
        </div>
    {% endfor %}
{% endcommentList %}

By modifyingorderParameter, you can easily adjust the display order of comments to meet the interactive needs of the website.

Implement pagination for the comment list.

For pages with a large number of comments, pagination is an essential feature, as it can prevent the page from being too long, improve loading speed, and enhance user experience. Implementing comment pagination in AnQiCMS requirescommentListwith the tag andpaginationThe combination of tags.

  1. settingcommentListFor pagination mode: Firstly, incommentListIn the tag, you need totypethe parameter to"page", and uselimitThe parameter is used to specify how many comments to display per page. For example, display 10 comments per page:

    {% commentList comments with archiveId=archive.Id type="page" limit="10" order="id desc" %}
        {# 评论循环体 #}
    {% endcommentList %}
    

    HerecommentsThe variable will only contain the comment data for the current page.

  2. UsepaginationThe tag generates pagination navigation: IncommentListThe closing tag of the tag{% endcommentList %}After that, use it immediatelypaginationTags to render pagination navigation.paginationThe label will be based oncommentListThe total number of comments provided and the number of comments displayed per page, automatically generate "Previous page

    paginationA label usually needs oneshowThe parameter controls how many page number buttons are displayed in the pagination navigation, for exampleshow="5"this means that up to 5 page number buttons are displayed.

    A complete pagination navigation example code may look like this:

    <div>
        {% pagination pages with show="5" %}
            <ul>
                <li>总数:{{pages.TotalItems}}条,总共:{{pages.TotalPages}}页,当前第{{pages.CurrentPage}}页</li>
                <li class="{% if pages.FirstPage.IsCurrent %}active{% endif %}"><a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a></li>
                {% if pages.PrevPage %}
                    <li><a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a></li>
                {% endif %}
                {% for item in pages.Pages %}
                    <li class="{% if item.IsCurrent %}active{% endif %}"><a href="{{item.Link}}">{{item.Name}}</a></li>
                {% endfor %}
                {% if pages.NextPage %}
                    <li><a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a></li>
                {% endif %}
                <li class="{% if pages.LastPage.IsCurrent %}active{% endif %}"><a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a></li>
            </ul>
        {% endpagination %}
    </div>
    

    pagesThe variable contains all the information needed for pagination, such as total number of items, total number of pages, current page number, and links to each page, you can build a user-friendly pagination interface through loops and conditional judgments.

Combined example: Sorting and pagination applied simultaneously

Combine sorting and pagination features, yourcomment/list.htmlThe relevant code snippet in the file may look like this:

{# 评论列表部分 #}
{% commentList comments with archiveId=archive.Id type="page" limit="10" order="id desc" %}
    <div class="comment-list-container">
        {% for item in comments %}
            <div class="comment-item">
                <span class="comment-author">{{item.UserName}}</span>
                <span class="comment-time">{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
                {% if item.Status == 1 %} {# 确保只显示已审核的评论 #}
                    <p class="comment-content">{{item.Content}}</p>
                    {% if item.Parent %}
                        <blockquote class="reply-quote">回复 @{{item.Parent.UserName}}: {{item.Parent.Content}}</blockquote>
                    {% endif %}
                {% else %}
                    <p class="comment-content">评论正在审核中...</p>
                {% endif %}
                {# 其他评论操作,如点赞、回复等 #}
            </div>
        {% endfor %}
    </div>
{% empty %}
    <p class="no-comments">暂无评论,快来发表您的看法吧!</p>
{% endcommentList %}

{# 分页导航部分 #}
<div class="pagination-container">
    {% pagination pages with show="5" %}
        <nav aria-label="Page navigation">
            <ul class="pagination">
                {% if pages.FirstPage and not pages.FirstPage.IsCurrent %}
                    <li class="page-item"><a class="page-link" href="{{pages.FirstPage.Link}}">首页</a></li>
                {% endif %}
                {% if pages.PrevPage %}
                    <li class="page-item"><a class="page-link" href="{{pages.PrevPage.Link}}">上一页</a></li>
                {% endif %}
                {% for item in pages.Pages %}
                    <li class="page-item {% if item.IsCurrent %}active{% endif %}"><a class="page-link" href="{{item.Link}}">{{item.Name}}</a></li>
                {% endfor %}
                {% if pages.NextPage %}
                    <li class="page-item"><a class="page-link" href="{{pages.NextPage.Link}}">下一页</a></li>
                {% endif %}
                {% if pages.LastPage and not pages.LastPage.IsCurrent %}
                    <li class="page-item"><a class="page-link" href="{{pages.LastPage.Link}}">尾页</a></li>
                {% endif %}
            </ul>
        </nav>
    {% endpagination %}
</div>

By such configuration, you can provide an orderly and easy-to-navigate comment section for your AnQiCMS website, greatly enhancing user participation and the professionalism of the website.Remember to clear the system cache after each modification of the template file to ensure that your changes take effect immediately.

Frequently Asked Questions (FAQ)

How to control the review status of comments to ensure that only approved comments are displayed?

In AnQiCMS, comments will have a certain status after being published.StatusThe field indicates its review status. In the loop of the comment list, you can judgeitem.StatusThe value to decide whether to display the comment. Usually,Status = 1Indicates that the review has passed. It already includes the examples above.{% if item.Status == 1 %}The condition judgment ensures that only comments that have passed the review are displayed with specific content, otherwise the prompt 'Comments are being reviewed...' is displayed.The background "Content Comment" feature module allows you to manually review, delete, and other operations on comments.

2. Can you display a list of comments for different articles on the same page?

Can.commentListlabel'sarchiveIdThe parameter allows you to specify the article ID to retrieve comments. If you need to display comments lists for different articles on the same page (for example, showcasing the latest comments of multiple related articles on a special page), you just need to specify each one as needed.commentListPass the corresponding article ID. For example:

{# 文章A的评论 #}
{% commentList commentsA with archiveId=123 type="list" limit="5" order="id desc" %}...{% endcommentList %}

{# 文章B的评论 #}
{% commentList commentsB with archiveId=456 type="list" limit="5" order="id desc" %}...{% endcommentList %}

3. How to ensure the performance of loading and pagination of the comment list?

AnQiCMS is developed based on Go language, which has a natural advantage in data processing and concurrency. For the comment list, performance optimization mainly focuses on the following points:

  • Reasonable settingslimitParameterDo not load too many comments at once, keep the number of comments displayed per page reasonable (such as 10-20), and reduce the amount of data requested in one go.
  • Database indexEnsure that the comments tablearchive_idandidThe field has an appropriate database index, AnQiCMS will usually handle this automatically.
  • Template cache: AnQiCMS supports template caching, if the comment content does not change frequently, it can use template caching to reduce rendering overhead.
  • Front-end optimizationIf the number of comments is particularly large, consider combining with front-end lazy loading or Ajax dynamic loading technology to further optimize the user experience.

Related articles

How to filter and display AnQiCMS document list according to category ID and recommendation attributes?

Manage website content in AnQiCMS, often need to display document lists according to different conditions.Whether you want to display the latest articles of a specific category on the homepage or highlight some recommended content in the sidebar, the system provides flexible tools to help us achieve this.Today, let's talk about how to cleverly use category ID and recommendation attributes to accurately filter and display the document list you want.

2025-11-07

How to display category introduction, Banner image and thumbnail on AnQiCMS category page?

In website operations, the category page is not only an aggregation of content, but also an important entry for users to understand the website structure and explore specific topics in depth.A well-designed, informative category page that can significantly improve user experience and search engine optimization (SEO) effectiveness.AnQiCMS provides flexible features, allowing us to easily display category descriptions, banner images, and thumbnails on the category page, giving your website's category page a fresh look.Understanding AnQiCMS's category page and template structure In AnQiCMS

2025-11-07

How to display AnQiCMS single-page content and related images?

In website operation, we often need to create some independent pages, such as "About UsThese pages contain relatively fixed content, do not belong to the conventional article or product list, AnQiCMS (AnQiCMS) classifies them as "single page" and provides a very convenient management and display method.Through this system, you can easily publish and update this content, while flexibly controlling their display styles and image presentation.

2025-11-07

How to display custom Banner images on AnQiCMS pages?

In website operation, skillfully using Banner images can not only beautify the page but also effectively convey information and guide user behavior.AnQiCMS as a flexible content management system, provides various ways to display customized Banner images, allowing you to configure them individually according to different pages and needs. ### Flexible Upload and Management of Banner Images Firstly, all images that are going to be used as banners need to be uploaded to the AnQiCMS media library.This is very simple, you can go to the 'Page Resources' menu in the backend

2025-11-07

How to use AnQiCMS tags to display categories and documents in the navigation menu?

In AnQiCMS, the website's navigation menu is not only an important entry for users to explore content and understand the website structure, but it is also a key for search engines to crawl and understand the website's hierarchical relationships.Using the powerful template tag feature of AnQiCMS, we can easily display categories and documents dynamically in the navigation menu, thus building a beautiful and efficient website navigation. ### One, prepare the background work: build the navigation skeleton Before delving into the template code, we need to make some basic configurations in the AnQiCMS background.In fact, the data that the front-end tags can retrieve and display

2025-11-07

How to configure the page Title, Keywords, Description of AnQiCMS to optimize search engine display?

In website operation, making your content stand out in search engines is a key step to gaining traffic.And the page title (Title), keywords (Keywords), and description (Description), which we often call TDK, are important elements for communicating with search engines.AnQiCMS is a system focused on enterprise content management, fully aware of the value of TDK configuration for SEO, and therefore provides flexible and powerful functions to help us easily optimize the display of our website in search engines.### Why is TDK configuration so important

2025-11-07

How to use the Canonical URL feature of AnQiCMS to avoid content duplication display issues?

In website operations, we often encounter a tricky problem: content duplication.This does not refer to manually copying and pasting the article, but rather to the fact that the same content can be accessed through multiple different URL addresses due to various technical reasons.For search engines, this can cause confusion, as they are not sure which URL is the official version of the content, which may scatter the ranking weight of the page, reduce the efficiency of the crawler, and even affect the overall SEO performance of the website.Luckyly, AnQiCMS provides a powerful and convenient feature——Canonical

2025-11-07

How to use AnQiCMS built-in TDK tags to dynamically set page SEO information?

As an experienced website operator, I am well aware of the importance of Search Engine Optimization (SEO) for website visibility and traffic growth.In SEO work, the reasonable setting of TDK (Title, Description, Keywords) tags is undoubtedly the cornerstone of the cornerstone.A clear and accurate TDK that can not only help search engines better understand the page content, but also attract users to click on the search results.

2025-11-07