In content operation, the reasonable layout and statistics of keywords are an indispensable part of optimizing search engine performance and improving user experience.An accurate keyword distribution can not only help search engines better understand your content, but also allow users to find the information they need faster.AnQiCMS (AnQiCMS) provides us with a flexible way to count the occurrences of specific keywords in article content, thus assisting our content strategy, thanks to its powerful template engine.
Auto CMS does not directly provide a one-click statistics feature for the total number of keywords in the article keywords on the backend report, but its powerful and flexible template engine provides us with the tool to achieve this goal.countFilterThis filter can help us easily calculate the total number of times a specific keyword appears in the article content at the template level.
Why do we need to count the number of times a keyword appears?
Understand the frequency of keyword occurrence in the article has multi-dimensional significance for content operation:
- SEO optimization evaluationWe can avoid keyword stuffing (keyword padding) by counting keyword density, and also ensure that the core keywords are sufficiently mentioned to improve search engine rankings.
- Content quality controlWhether the keyword distribution is natural and reasonable directly affects the readability and user experience of the article. Too high or too low keyword frequency may affect the quality of the content.
- Content strategy adjustment:By analyzing the keywords of different articles, we can better understand which content themes need to be strengthened and which keywords perform well, thereby adjusting the future content creation direction.
Reveal the core features:countFilter
countThe filter is a practical tool in the Auto CMS template, its function is to calculate the number of times a keyword appears in a line of string or array. Its usage is intuitive and simple:
{{ obj|count:"关键词" }}
Among them,objRepresents the text content you want to perform keyword statistics on,"关键词"This is the specific word you want to count. For example, if you want to count the number of occurrences of the word 'CMS' in the string '欢迎使用安企CMS(AnQiCMS)', you can write it like this:
{{"欢迎使用安企CMS(AnQiCMS)"|count:"CMS"}}
This code will execute and the number will be displayed on the page.2Because it found 'CMS' twice.
Actual operation: Count keywords in a single article.
To count the number of occurrences of a specific keyword in the article detail page of the AnQi CMS, we first need to obtain the detailed content of the current article. This can be done byarchiveDetailtagsTo implement, then pass the article content incountFilter.
Assuming you want to count the occurrences of the keyword "AnQi CMS" in the article body on the article detail page:
{# 获取当前文章的完整内容 #}
{% archiveDetail articleContent with name="Content" %}
{# 统计“安企CMS”在文章内容中出现的次数 #}
<p>“安企CMS”在本文中出现了:{{ articleContent|count:"安企CMS" }} 次。</p>
{# 提示:如果文章内容包含HTML标签,并且您希望HTML标签内的文本也被计入,则上述代码可以直接使用。
如果您只希望统计纯文本内容,需要先用其他过滤器(如 striptags)移除HTML标签。 #}
Here is an important detail to note: the article content usually contains HTML tags.countThe filter will match the entire string (including HTML tags). If the keyword may appear inside HTML tags (such as<a href="/安企CMS">安企CMS</a>),or you do not want the HTML tags themselves to interfere with the statistics results, you can consider usingstriptagsFilterremove the HTML tags first and then count:
{# 获取当前文章的完整内容并移除HTML标签 #}
{% archiveDetail articleContent with name="Content" %}
{# 统计纯文本内容中“安企CMS”出现的次数 #}
<p>“安企CMS”在本文纯文本内容中出现了:{{ articleContent|striptags|count:"安企CMS" }} 次。</p>
Further: Counting keywords of multiple articles
If your goal is to count the number of occurrences of a specific keyword in multiple articles on a website, for example on an article list page or a custom aggregation page,archiveListtagsWithforThe loop will be your powerful assistant.
The following examples show how to count the occurrences of the keyword 'AnQiCMS' in each article of the article list:
{% archiveList archives with type="list" limit="10" %} {# 获取最新的10篇文章 #}
{% for item in archives %}
<div class="article-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
{# 直接使用 item.Content 来统计,item.Content 会自动包含文章的完整内容 #}
{% set keywordCount = item.Content|striptags|count:"安企CMS" %}
<p>关键词“安企CMS”在本文中出现了:{{ keywordCount }} 次。</p>
<p>{{ item.Description }}</p> {# 显示文章简介 #}
<a href="{{ item.Link }}" class="read-more">阅读更多</a>
</div>
{% else %}
<p>暂时没有文章。</p>
{% endfor %}
{% endarchiveList %}
In this example, we first go through:archiveListRetrieved a list of articles, then usingforLoop through each article one by one. Inside the loop, we directly accessitem.Contentto get the article content and pass it throughstriptagsFilter processed, then usedcountKeyword statistics are performed by the filter. The result is assigned tokeywordCounta variable to display on the page.
Advanced considerations and practical suggestions
- Case sensitivity:
countThe filter performs an exact match, default is case-sensitive.If you need to count without case sensitivity, you can consider converting the text content and keywords to a uniform case (such as all lowercase) before counting, but this requires customizing a case conversion filter in the Go language template engine. - Performance impactAlthough Go language's performance is very excellent, if you perform keyword statistics on the full content of a large number of articles on a single page, especially without using cache, it may have a certain impact on the page loading speed.Suggest to avoid complex real-time keyword statistics on high-traffic pages during traffic peak periods.
- Backend management functionPlease note that the method described here is to calculate the frequency of keyword occurrences in real time within the front-end template, not to generate a permanent statistical report in the background.The "Keyword Library Management" and "Site-wide Content Replacement" features provided by the Anqi CMS backend are administrative tools used for managing and optimizing website keywords. These are different application scenarios from the front-end statistics here.
- Keyword library linkageIf you have a keyword library and want to count the number of occurrences of all keywords in the keyword library, you need to first obtain the list of keyword libraries through other tags (such as custom tags, if available), then traverse the keyword libraries, and process the content of each article one by one.
countthe count.
Summary
The template engine of Anqi CMS provides high flexibility, even for seemingly complex keyword statistics needs, which can be achieved through the clever combination of existing tags and filters.Through this method, content operators can quickly obtain the keyword distribution information of the article content without relying on additional plugins or complex development, providing data support for content optimization.
Common Questions (FAQ)
Q1: Can this template-level keyword statistics be directly generated into a report on the Anqi CMS backend? A1:Cannot.The method described here is to calculate the frequency of keyword occurrences in real time through template code on the website front-end or preview page.The AutoCMS backend currently does not have a built-in function to directly generate keyword statistics reports.If you need backend reports, you may need to consider using external tools (such as Excel or data analysis scripts) after exporting the data for statistics.
Q2:countDoes the filter distinguish between uppercase and lowercase letters when counting keywords?
A2:Yes,countThe filter is case-sensitive by default.For example, "AnQi CMS" and "AnQi cms" are considered as different keywords.If you need to count without case sensitivity, the built-in filter of AnQi CMS cannot directly achieve this, and it may be necessary to create a custom Go language template filter to perform case conversion first.
Q3: If the article content is in Markdown format,countDoes the filter work properly?
A3:Generally speaking, when you call in the template of the Anqi CMS,item.ContentorarchiveDetail with name="Content"If the backend has enabled the Markdown editor, the system will first render the Markdown content into HTML.countThe filter will act on the rendered HTML text. If you only want to count plain text, it is recommended to use it firststriptagsThe filter will remove HTML tags and then proceedcountCount, to avoid the interference of HTML tag content in the results.