How to use the `wordcount` filter to display the word count of each article on the article list page?

Calendar 👁️ 74

When operating a website, we often need to understand some basic information about articles, such as reading time, word count, etc.This data not only helps us optimize content strategy, but also allows readers to have a preliminary judgment of the article while browsing.In AnQi CMS, by utilizing its powerful template filter function, we can easily display the word count of each article on the article list page.

The AnQi CMS template engine provides a rich set of filters (filters), which are like a toolbox for processing variables in templates, such as formatting dates, truncating strings, of course, including the one we are discussing today - word count. The syntax of using filters is very intuitive, usually adding a pipe symbol after the variable|Then follow the filter name, if parameters are needed, add a colon after the filter name.:And parameters.

The core tool for counting the number of words in articles iswordcountA filter. As the name suggests,wordcountThe filter's role is to count the number of words or characters in the text.It will traverse the input string and count the number of 'words' based on spaces and other delimiters.For Chinese content, a character is usually considered a 'word' or at least a counting unit;For English content, count the words according to the word.

We need to edit the corresponding list page template file to display the word count of each article on the article list page. These template files are usually located in/template/{您的模板目录}/{模型table}/list.htmlor a similar path (for exampledefault/article/list.html)

The steps to implement this feature are as follows: First, we need to usearchiveListTag to get the article list data. This tag can help us retrieve articles that meet the conditions from the database and organize them into a collection that can be iterated over by the template.archiveListWithin the tag, we will useforto process each article one by one.

InforIn the loop, the data of each article will be assigned to a variable (usuallyitem). The content of the article is stored initem.ContentThis field contains. At this point, we only need to simplywordcounta filter toitem.Contentto get the number of words in the article.

For example, you can add the following code snippet to the article list page template:

{# 假设这是您的文章列表页模板文件,例如:default/article/list.html #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article class="article-item">
        <h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
        <div class="article-meta">
            <span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{{item.Views}}</span>
            {# 这里是核心:使用 wordcount 过滤器统计文章字数 #}
            <span>字数:{{ item.Content|wordcount }}</span>
        </div>
        <div class="article-description">
            <p>{{item.Description}}</p>
        </div>
        <a href="{{item.Link}}" class="read-more">阅读全文</a>
    </article>
    {% empty %}
    <p>暂时没有文章发布。</p>
    {% endfor %}

    {# 如果您使用了分页功能,还需要添加分页代码 #}
    {% pagination pages with show="5" %}
        {# 这里放置您的分页链接代码,例如: #}
        <div class="pagination-links">
            {% if pages.FirstPage %}<a href="{{pages.FirstPage.Link}}">首页</a>{% endif %}
            {% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
            {% for page_item in pages.Pages %}
                <a class="{% if page_item.IsCurrent %}active{% endif %}" href="{{page_item.Link}}">{{page_item.Name}}</a>
            {% endfor %}
            {% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
            {% if pages.LastPage %}<a href="{{pages.LastPage.Link}}">尾页</a>{% endif %}
        </div>
    {% endpagination %}
{% endarchiveList %}

In this code block,{{ item.Content|wordcount }}It is the key to implementing word count. It will retrieve the content of the current article.item.ContentThen proceedwordcountThe filter calculates the number of words and displays them directly. In this way, you can add a practical and informative element to your article list, enhancing the user experience.

Frequently Asked Questions (FAQ)

Q1: Why did I modify the template file and addwordcountthe filter, and the front-end page did not take effect immediately?

A1:The AQCMS usually enables template caching to improve website access speed.After you modify the template file, the browser may still load the old cached file, or the server may not update the cache in time.You can try to clear the cache in the "System Settings" or "Update Cache" feature on the AnqiCMS backend, and then force refresh the browser (Ctrl+F5 or Cmd+Shift+R).

Q2:wordcountWhat is the filter counting, the number of Chinese characters or English words? Will the inclusion of HTML tags in the article content affect the counting results?

A2: wordcountThe filter will differentiate 'words' based on spaces in the text and count them.For English, it can accurately count the number of words; for Chinese, because Chinese characters are usually not separated by spaces, it often counts the number of individual Chinese characters.If your article contentitem.ContentContains a large number of HTML tags (such as<p><strong>内容</strong></p>)wordcountThe filter will also count these tag texts, which may not be the 'visible word count' you want. To get a more accurate pure text word count, you can use it beforewordcount.striptagsFilter, for example{{ item.Content|striptags|wordcount }}This can remove all HTML tags first, then count the number of pure text characters.

Q3: Besides the article list page, I can use it in other places.wordcountAre you looking for a filter? Like the article detail page?

A3:Of course. As long as you can get a string variable containing the article content in the template, you can use it.wordcountFilter. On the article detail page (usuallydefault/article/detail.htmlor similar files), the article content is generally obtained througharchive.Contentor{% archiveDetail with name="Content" %}. You can directly apply it to the detail page, for example文章字数:{{ archive.Content|wordcount }}To display word count information at the top or bottom of the article.

Related articles

How does the `wordcount` filter define the boundary of a 'word' when counting Chinese content?

In daily website content operation, we often need to count and control the length of content, which is crucial for SEO, layout and user reading experience.The AnqiCMS provides a series of practical template filters to help us complete these tasks, where `wordcount` is a tool used to count the number of 'words' in a string.However, how is the boundary of the 'word' defined for Chinese content?This may be a question that many users encounter when using it.

2025-11-09

How to accurately calculate the word count of article content in AnQiCMS templates?

How to easily count the word count of article content in AnQiCMS template?Practical methods and skills Understanding the word count or number of words in articles is a basic and important indicator in content operation.It is very helpful to accurately count the length of article content for SEO optimization, cost assessment of content creation, or to ensure that the content complies with platform standards.

2025-11-09

How to remove extra blank lines or newline characters in the output content by using the `remove` tag in AnQiCMS?

While developing website templates with AnQiCMS, we often encounter a detail issue that the logic tags (such as `if` condition judgment, `for` loop) in the template may generate unnecessary blank lines or newline characters when rendering output HTML content. These seemingly harmless blanks usually do not affect the final visual presentation of the page, but in some scenarios where code neatness is required or precise control of element spacing is needed, they may bring some minor troubles, such as affecting the readability of the HTML source code

2025-11-09

How to accurately control the timestamp date format display in the `stampToDate` tag of AnQiCMS on the front-end?

Clear, consistent, and beautiful time information display is crucial for user experience in website operation.Whether it is the publication date of the article, the update time of the product, or the deadline of the event, a friendly date and time format always allows visitors to obtain information more quickly.AnQiCMS deeply understands this, providing us with a powerful and flexible template tag - `stampToDate`, which allows us to easily control various date and time formats displayed on the front end.

2025-11-09

AnQiCMS blog article detail page, how to display the total word count of the current article in real time?

In content operation, the word count (or number of words) of an article is often an important indicator for measuring the depth of content, the estimated reading time, and even search engine optimization (SEO).For those of us who use AnQiCMS to manage website content, being able to display the total word count of the current article in real-time on the blog article detail page would undoubtedly enhance user experience and provide more intuitive data reference for website operation.AnQiCMS powerful template system and built-in features make it simple and efficient to meet this requirement.

2025-11-09

What is the core difference between the `wordcount` filter and the `length` filter in calculating text length?

In AnQi CMS template development, we often need to process and count text content, including calculating the length of the text.The system provides the `wordcount` and `length` filters, which both help us measure text, but they have different focuses and application scenarios.Understanding the core differences can make us more proficient in practical use, writing more efficient and accurate template code.

2025-11-09

Can I use the `wordcount` filter to check if the user's submitted content meets the minimum word count requirement?

In the daily content operation of AnQiCMS (AnQiCMS), we often encounter the need to limit the number of words or characters in user-submitted content to ensure the quality and standardization of the content.So, can the `wordcount` filter provided by Anqi CMS help us achieve this goal?This article will delve into the functionality, usage, and application of the `wordcount` filter in content management, and point out its limitations, helping you better utilize this tool.

2025-11-09

How to use the `wordcount` result in AnQiCMS for conditional judgment, such as displaying different prompts based on the word count?

In AnQi CMS, implementing dynamic content display is an important factor in improving user experience and website professionalism.Sometimes, we may want to display different prompts based on the length of the article content, such as prompting 'Quick Read' for short articles, while estimating reading time for long content.The powerful template engine and built-in filters of AnQi CMS make everything easy.This article will deeply explore how to skillfully use the `wordcount` filter in AnQiCMS combined with conditional judgment to implement a dynamic prompt function based on the word count of the content

2025-11-09