How to implement the pagination display function of the article comment list?

Calendar 👁️ 62

In website operation, efficiently managing and displaying user comments is crucial for enhancing user interaction and content vitality.AnQiCMS (AnQiCMS) is a powerful content management system that provides flexible template tags, allowing us to easily implement pagination of article comment lists.This article will introduce in detail how to implement this function through template tags in Anqi CMS, thereby providing users with a better browsing experience.

Understanding the core of comments and pagination

In the Anqi CMS template system, the pagination display of article comment lists mainly depends on two core template tags: commentListandpagination.

  • commentListTagUsed to retrieve the comment data for a specified article. It supports various parameters, such as filtering comments through the article ID (archiveId)type="page"Enable pagination mode and set the number of comments displayed per page (limit).
  • paginationTag: It is used specifically for generating pagination navigation. It can according tocommentListTag gets the pagination data, automatically generates "Previous page", "Next page", and page number links.

By combining these two tags, we can build a fully functional and user-friendly comment pagination system.

Step 1: Prepare the comment list template

usually, the comment list is placed on the article detail page (archive/detail.html) or in a separate comment list template file (comment/list.htmlIn any case, we need a container to hold the comment content and pagination navigation.

If you want to use a separate template file for the comment list, you can refer to the Anqi CMS template production conventions incomment/list.htmlName specification. It is more common to integrate the comment function directly into the article detail page.

Step 2: Call the comment data and enable pagination.

To display the comments of an article, we need to obtain the current article ID and pass it tocommentListthe tag. In the article detail page of Anqi CMS, the information of the current article is usually obtained througharchiveThe object is provided directly.

The basic structure of the comment data call is as follows:

{# 假设这是文章详情页 (archive/detail.html) 或其他包含评论的模板 #}

<div class="comment-list-container">
    <h3>用户评论 ({{ archive.CommentCount }})</h3> {# 显示评论总数 #}
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {% for item in comments %}
        <div class="comment-item">
            <div class="comment-meta">
                <span class="username">
                    {% if item.Status != 1 %}
                    待审核用户: {{item.UserName|truncatechars:6}}...
                    {% else %}
                    {{item.UserName}}
                    {% endif %}
                </span>
                {% if item.Parent %}
                <span class="reply-to">回复 {% if item.Parent.Status != 1 %}待审核用户: {{item.Parent.UserName|truncatechars:6}}...{% else %}{{item.Parent.UserName}}{% endif %}</span>
                {% endif %}
                <span class="comment-time">{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span>
            </div>
            <div class="comment-content">
                {% if item.Parent %}
                <blockquote class="reply-quote">
                    {% if item.Parent.Status != 1 %}
                    <p>该内容正在审核中...</p>
                    {% else %}
                    <p>{{item.Parent.Content|truncatechars:100}}</p>
                    {% endif %}
                </blockquote>
                {% endif %}
                {% if item.Status != 1 %}
                <p class="moderation-pending">该评论正在审核中,审核通过后将显示。</p>
                {% else %}
                <p>{{item.Content}}</p>
                {% endif %}
            </div>
            {# 如果需要点赞功能,可以放置此处 #}
            <div class="comment-actions" data-id="{{item.Id}}" data-user="{{item.UserName}}">
                <a class="like-btn" data-id="praise">赞 ({{item.VoteCount}})</a>
                <a class="reply-btn" data-id="reply">回复</a>
            </div>
        </div>
        {% empty %}
        <p class="no-comments">暂无评论,快来发表你的看法吧!</p>
        {% endfor %}
    {% endcommentList %}
</div>

Code explanation:

  • archiveId=archive.Id: This is crucial, it passes the current article's ID tocommentListto ensure that only comments related to the current article are displayed.archiveThis is the current article object automatically provided by AnQi CMS on the article detail page.
  • type="page": Tell the system that we need pagination mode instead of simple list mode.
  • limit="10": Set the number of comments displayed per page. You can adjust this number according to your actual needs.
  • {% for item in comments %}: Loop through the comments data obtained.
  • item.UserName,item.CreatedTime,item.Content: Retrieve the username, creation time, and comment content of the comments.
  • item.Status: Used to determine if a comment has passed the review, if the comment is set to review status in the background, it can prompt the user before it is approved.
  • item.ParentThis field is provided to support multi-level comments (reply comments). If it exists, it indicates that the current comment is a reply to another comment.
  • stampToDateA helper tag for formatting timestamps, converting timestamps to a readable date format.

Step 3: Build pagination navigation

IncommentListAfter the loop ends, use it immediately.paginationtags to generate pagination navigation.

{# 紧接在 commentList 标签之后 #}
<div class="pagination-container">
    {% pagination pages with show="5" %}
    <ul>
        {# 首页链接 #}
        <li class="page-item {% if pages.FirstPage.IsCurrent %}active{% endif %}">
            <a href="{{pages.FirstPage.Link}}">{{pages.FirstPage.Name}}</a>
        </li>
        {# 上一页链接 #}
        {% if pages.PrevPage %}
            <li class="page-item">
                <a href="{{pages.PrevPage.Link}}">{{pages.PrevPage.Name}}</a>
            </li>
        {% endif %}
        {# 中间页码链接 #}
        {% for page_item in pages.Pages %}
            <li class="page-item {% if page_item.IsCurrent %}active{% endif %}">
                <a href="{{page_item.Link}}">{{page_item.Name}}</a>
            </li>
        {% endfor %}
        {# 下一页链接 #}
        {% if pages.NextPage %}
            <li class="page-item">
                <a href="{{pages.NextPage.Link}}">{{pages.NextPage.Name}}</a>
            </li>
        {% endif %}
        {# 尾页链接 #}
        <li class="page-item {% if pages.LastPage.IsCurrent %}active{% endif %}">
            <a href="{{pages.LastPage.Link}}">{{pages.LastPage.Name}}</a>
        </li>
    </ul>
    <p class="pagination-info">
        共 {{pages.TotalItems}} 条评论,{{pages.TotalPages}} 页,当前第 {{pages.CurrentPage}} 页
    </p>
    {% endpagination %}
</div>

Code explanation:

  • pagesThis iscommentListIntype="page"Automatically generated object containing pagination information in mode.
  • show="5": indicates that up to 5 page number buttons are displayed in the middle page number section, for example... 2 3 4 5 6 ....
  • pages.FirstPage.Link/pages.PrevPage.Linketc.: to get the links of Home, Previous Page, Next Page, and Last Page.
  • pages.PagesThis is an array that contains all the middle page number information, through the loop{% for page_item in pages.Pages %}to display the specific page number.
  • page_item.IsCurrent: It is used to determine whether the current page number is the one being viewed, and it is usually given anactiveclass to highlight.

Integration of code examples

Place the above two code snippets (comment list and pagination navigation) in order in your template file to implement the complete comment pagination feature. For example, indefaultTemplate'sarchive/detail.htmlfile:

`twig {# ... article details ... #}

<div class="comment-list-container">
    <h3>用户评论 ({{ archive.CommentCount }})</h3>
    {% commentList comments with archiveId=archive.Id type="page" limit="10" %}
        {% for item in comments %}
        <div class="comment-item">
            <div class="comment-meta">
                <span class="username">
                    {% if item.Status != 1 %}
                    待审核用户: {{item.UserName|truncatechars:6}}...
                    {% else %}
                    {{item.UserName}}
                    {% endif %}
                </span>
                {% if item.Parent %}
                <span class="reply-to">回复 {% if item.Parent.Status != 1 %}待审核用户: {{item.Parent.UserName|truncatechars:6}}

Related articles

How to use Tag tags to associate and display related content on the front end?

In Anqi CMS, content organization and association is the key to enhancing website value, optimizing user experience, and improving search engine performance.In addition to the traditional classification structure, we also have a very powerful and flexible tool - Tag tag.It is like a 'living index' of content, which helps us to connect scattered content in multiple dimensions, making it easier for users to find the information they are interested in.Today, let's talk about how to make full use of Tag tags on the front end to associate and display our wonderful content.### Tag label: Flexible content association bridge Different from a strict classification system

2025-11-08

How to display the content of custom fields (such as author, source) on the article detail page?

In website operation, the article detail page is not only a place to display the core content, but also a key to convey additional information, enhance professionalism, and improve user experience.In addition to the title and body, we often need to display some custom fields on the article detail page, such as author, source, publisher, product model, and so on.This information not only enriches the content of the article, but also helps to enhance the authority and credibility of the website, even having a positive impact on SEO optimization and user decision-making.AnQiCMS (AnQiCMS) is designed with a flexible content model, making the display of these custom fields very intuitive and powerful

2025-11-08

How to get and display a list of recommended articles related to the current article?

In website operation, when we have worked hard to create an article, we naturally hope that it can be seen by more readers and guide them to explore more exciting content.On the bottom or sidebar of the article detail page, cleverly display recommended content related to the current article, which can not only significantly improve the depth and dwell time of users' reading, but also bring many benefits to search engine optimization (SEO).AnQiCMS (AnQiCMS) fully understands this need and provides extremely convenient and powerful tags to help us easily achieve this goal.### Cleverly Utilize `archiveList`

2025-11-08

How to display the link to the previous and next article on the article detail page?

It is an important link in the article detail page of AnQi CMS to display "Previous" and "Next" navigation links, which helps improve the user experience and the internal link structure of the website.This not only guides readers to continue browsing related content and extend their stay time, but also helps search engines better understand the relevance between website content, which has a positive effect on SEO optimization.AnQi CMS provides a user-friendly and powerful template tag system

2025-11-08

How to display the message form submitted by users on the website?

A highly efficient and flexible mechanism is provided by Anqi CMS to easily display and manage user message forms on your website.Whether it is to collect user feedback, consultation, or simple interactive messages, Anqi CMS can help you quickly implement this function and customize it according to your business needs. ### Learn about the Anqi CMS message function In the Anqi CMS backend management interface, you can find the "Website Message" option under the "Function Management" menu.This is the core area where you manage all comment-related settings, including viewing submitted comments and customizing form fields

2025-11-08

How to call and display the friendship link set in the background on the front end?

AnQiCMS (AnQiCMS) provides a set of intuitive and powerful content management features, among which the management of 'friend links' is an indispensable part of website operation. It not only promotes the SEO performance of the website but also provides users with convenient navigation.This article will give a detailed introduction on how to call and elegantly display the friendship links you set in the background. ### Friend link backend configuration Managing friend links in Anqi CMS is very simple.You just need to log in to the back-end, find the "Function Management" module in the left menu bar, click to enter "Friend Links". Here

2025-11-08

How to implement pagination navigation display for content list in AnQi CMS?

When using AnQiCMS to build a website, it is particularly important to have reasonable pagination navigation when displaying a large amount of content, such as article lists, product lists, etc.It not only enhances user experience, making it easier for visitors to browse and find content, but also helps search engines better crawl the website.AnQi CMS provides a very intuitive and powerful pagination tag, making it easy to display content list pagination.### Step 1: Prepare the content list data To implement pagination of the content list, you first need to use `archiveList`

2025-11-08

How to embed mathematical formulas and flowcharts in a page and ensure correct rendering display?

In website operation, we often need to display some professional content, such as mathematical formulas involving scientific calculations, or complex flowcharts for business process descriptions.The traditional way of displaying this content is often inefficient, difficult to maintain, and also does not provide a good reading experience.AnQiCMS combines its powerful content management capabilities and support for Markdown to provide us with an elegant solution.By simply introducing some external libraries and enabling the Markdown editor, we can display the professional content correctly on the website page

2025-11-08