How to count the number of times a specific keyword appears in the title or description of an article?

Calendar 👁️ 88

As Anqicms users, we often need to manage and analyze website content in a refined manner, one of the common requirements is to count the frequency of specific keywords in article titles or descriptions.This not only helps us understand the effectiveness of content marketing, optimize SEO strategies, but also better grasp user focus points.AnQi CMS, with its flexible template engine and rich built-in filters, makes this operation feasible at the front-end template level.

We need to utilize the powerful functions of the Anqi CMS template system, especially its 'filter' mechanism, in order to count the number of times a specific keyword appears in the article title or description.The template language of AnQi CMS is similar to Django, by cleverly combining tags and filters, we can dynamically calculate and display these data when the page is loaded.

Understanding Anqi CMS templates and content data

In Anqi CMS, articles and their related information (such as titles, descriptions) are usually througharchiveListorarchiveDetailTags to retrieve. These tags will return one or more article objects, each of which includes various properties of the article, such asTitle(Article Title) andDescription(Article description). Our statistical work will focus on these properties.

The Anqi CMS template engine provides a variety of built-in filters that can perform various operations on variables, including string operations and format conversions. Among them,countThe filter is the core tool we use to implement keyword statistics.It can calculate the number of times a substring appears in a line of text, or the number of times an element appears in an array.

UsecountFilter keyword statistics

Let us take the example of counting the frequency of the keyword "AnQi CMS" in the article title and description. Suppose we are editing a template file for an article list page (such aslist.htmlorcategory.htmlOr a detailed article page (for example)detail.html)

Firstly, we can define a variable to store the keywords we want to count, which is convenient for management and modification:

{% set target_keyword = "安企CMS" %}

Next, when we iterate over the article list, we can apply the filter to the title and description of each articlecountfilter.

{% archiveList articles with type="page" limit="10" %}
    {% for article in articles %}
        <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
        {% set title_occurrences = article.Title|count:target_keyword %}
        {% if title_occurrences > 0 %}
            <p>关键词 "{{ target_keyword }}" 在标题中出现 {{ title_occurrences }} 次。</p>
        {% endif %}

        <p>描述:{{ article.Description }}</p>
        {% set description_occurrences = article.Description|count:target_keyword %}
        {% if description_occurrences > 0 %}
            <p>关键词 "{{ target_keyword }}" 在描述中出现 {{ description_occurrences }} 次。</p>
        {% endif %}
        <hr>
    {% empty %}
        <p>暂无文章。</p>
    {% endfor %}
    {% pagination pages with show="5" %}
        {# 这里是分页导航的代码 #}
    {% endpagination %}
{% endarchiveList %}

In this code block:

  • We first usearchiveListTag to get the article list.
  • Inforthe loop,article.Titleandarticle.DescriptionRepresent the title and description of the current article.
  • |count:target_keywordThe filter will calculatetarget_keywordInarticle.Titleorarticle.DescriptionThe number of times a string appears as a substring in the string, and assign the result totitle_occurrencesordescription_occurrencesVariable.
  • We use{% if ... %}Logical judgment tag, only when the keyword appears more than 0 times, will the statistical results be displayed on the page, maintaining the page's cleanliness.

Advanced Application: Handling Case-Insensitive and More Precise Word Statistics

The AbovecountThe filter is case-sensitive by default, which means that "CMS" and "cms" are considered different keywords. If we need to perform case-insensitive statistics, we can cleverly combinelowerThe filter, which converts the article title/description and target keywords to lowercase, and then performs the count:

{% set target_keyword = "anqicms" %} {# 目标关键词统一使用小写 #}

{% archiveList articles with type="page" limit="10" %}
    {% for article in articles %}
        <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
        {% set article_title_lower = article.Title|lower %} {# 将标题转为小写 #}
        {% set title_occurrences_insensitive = article_title_lower|count:target_keyword %}
        {% if title_occurrences_insensitive > 0 %}
            <p>关键词 "{{ target_keyword }}" (不区分大小写) 在标题中出现 {{ title_occurrences_insensitive }} 次。</p>
        {% endif %}
        {# 描述部分同理 #}
        <hr>
    {% endfor %}
{% endarchiveList %}

If you want to implement a more strict 'complete word' count (instead of substrings), for example, 'CMS' should only count the independent 'CMS' words, not the 'CMS' part in 'AnQiCMS', which would be slightly more complex in the template layer of AnQiCMS because the template filter does not directly support complex regular expressions. One workaround is to first usesplitThe filter splits the title or description into an array of words, then traverses the array for exact matching and counting. However, this significantly increases the logical complexity and computational volume of the template. In most cases, for SEO keyword density analysis, the substring'scountIt is already sufficient to meet the needs.

How to operate in Anqi CMS

  1. Log in to the backend:Enter your Anqi CMS management background.
  2. Navigate to template design:On the left

Related articles

How to automatically convert line breaks in user input plain text content to the HTML `<br/>` tag?

In daily website content operations, we often need to enter some multi-line text content in the Anqi CMS backend, such as the introduction of articles, the characteristics of products, or detailed descriptions in custom fields.This content is usually entered in plain text format, however, when we expect them to be displayed on the front-end web page with the original paragraph and line break effects, we find that they are often cramped into a line, losing their original formatting.This is not a problem with the AnQi CMS system itself, but a characteristic of the HTML web page rendering mechanism.in HTML

2025-11-09

How to prevent XSS attacks and correctly escape HTML special characters when outputting article content in a template?

Managing and displaying content in AnQiCMS is the core task of website operation, but it is also crucial to ensure that these contents are presented safely and securely to users.Among them, cross-site scripting (XSS) attacks are a risk that should not be ignored, which may allow malicious code to be executed in the user's browser accessing your website, thereby triggering a series of security issues, such as stealing user data, tampering with page content, and even hijacking user sessions.

2025-11-09

Extract the article summary by word count instead of character count, which filter should be used in the template?

In website content operation, the way articles are presented often directly affects readers' click intentions and reading experience.A clear and concise summary that helps readers quickly understand the main theme of the article.Especially in scenarios such as list pages and recommendation areas, we usually hope that the abstract can be intelligently extracted, controlling the length while maintaining readability.When it comes to generating abstracts based on the content of an article, the length of the excerpt is a core consideration.There are two common ways to truncate: by character count and by word count.Cutting by character count is accurate, but it often encounters the situation where words are abruptly cut off

2025-11-09

How to safely extract the content of an article containing HTML tags without destroying its structure?

In website content management and display, we often need to display the partial content of articles in article lists, homepage summaries, or related recommendation areas to attract readers to click.However, directly truncating the content of an article containing HTML tags can easily disrupt the original HTML structure, causing the page to display incorrectly and even affecting the overall layout and user experience.AnQiCMS (AnQiCMS) fully understands this pain point, its powerful template engine and built-in filters provide an elegant and secure solution, allowing you to worry-free about display issues caused by content truncation. Next

2025-11-09

How to configure multiple sites in AnQiCMS to achieve independent display of content under different domain names?

AnQiCMS provides powerful multi-site management features, allowing you to easily operate multiple independent websites on the same system, each with its own domain and content.This is undoubtedly a very convenient and efficient solution for operators who have multiple brands, sub-sites, or need to provide differentiated content for different audience groups.Through a unified backend management entry, you do not have to deploy a separate system for each site, which greatly simplifies the operation and maintenance work, while also ensuring the complete independence of the front-end content display.Next, we will discuss in detail how to use AnQiCMS

2025-11-09

How to customize a content model for specific business needs and elegantly display its content on the front end?

In website content operation, we often encounter situations where we need to display diverse information.Traditional CMS may only provide fixed types such as 'articles' or 'products'. When business needs become more complex, such as displaying real estate information, job openings, event schedules, etc., these fixed types often seem inadequate.At this time, the flexible content model function of AnQiCMS is particularly important, it allows us to tailor the data structure to meet specific business needs and present the content in the way we want on the front end.Why do we need a custom content model?Imagine

2025-11-09

How does AnQiCMS support the switching and correct display of multilingual content?

AnQiCMS demonstrates excellent support capabilities in global content promotion. It provides a flexible mechanism to help us switch between multilingual content and display it correctly, thereby effectively reaching user groups from different language backgrounds. ### Core Mechanism: Making Content Speak Multiple Languages AnQiCMS's multilingual support is not just about translating the website interface, but also deeply involves the organization and presentation strategies of content.In its design philosophy, the implementation of multilingualism is mainly reflected in two levels: one is the localization processing of static text at the system level and template level.

2025-11-09

How to optimize URL structure through pseudo-static rules to improve the display and crawling of content by search engines?

In website operation, a clear, search engine friendly URL (Uniform Resource Locator) is crucial for the display and crawling of content.AnQiCMS (AnQiCMS) provides powerful pseudo-static rule management functions, allowing us to flexibly optimize the URL structure of the website, thereby effectively enhancing the performance of search engines.### Why is URL structure important?First, let's understand why the structure of URL is so crucial for the SEO (Search Engine Optimization) of a website. 1.

2025-11-09