In content operation, the reasonable layout and statistics of keywords are an indispensable part of optimizing search engine performance and improving user experience.A precise keyword distribution can not only help search engines better understand your content, but also allow users to find the information they need more quickly.AnQiCMS, with its powerful template engine, provides us with a flexible way to count the occurrences of specific keywords in the content of articles, thereby assisting our content strategy.
AnQi CMS does not provide a direct function to quickly count the total number of keywords in the articles through the background 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 keywords appearing in the article, which has many aspects of significance for content operation:
- SEO optimization evaluationWe can avoid keyword stuffing (keyword padding) by counting keyword density, and ensure that 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. Both too high and too low keyword frequencies may affect the quality of the content.
- Content strategy adjustmentBy analyzing the keyword statistics of different articles, we can better understand which content themes need to be strengthened and which keywords perform well, thus adjusting the future direction of content creation.
The core feature revelation:countFilter
countThe filter is a practical tool in Anqie CMS template, its function is to calculate the number of times a keyword appears in a line string or array. Its usage is intuitive and simple:
{{ obj|count:"关键词" }}
Among them,objRepresent the text content you want to perform keyword statistics on, and"关键词"Then it is the specific word you want to count. For example, if you want to count the number of times the word “CMS” appears in the string “欢迎使用安企CMS(AnQiCMS)”, you can write it like this:
{{"欢迎使用安企CMS(AnQiCMS)"|count:"CMS"}}
This code executes after, the number will be displayed on the page2Because it found the word "CMS" twice
Actual operation: Count keywords of a single article
In order to count the occurrences of specific keywords on the article detail page of AnQi CMS, we first need to obtain the detailed content of the current article. This can be done byarchiveDetailTagTo implement, then pass the article content incountfilter.
Assuming you want to count the occurrences of the keyword "AnQiCMS" 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标签。 #}
This is an important detail: the article content usually includes HTML tags.countThe filter will match the entire string (including HTML tags). If the keyword may appear inside HTML tags (for example<a href="/安企CMS">安企CMS</a>),or you do not want the HTML tags themselves to interfere with the statistical results, you can consider using them firststriptagsFilterRemove the HTML tags and then count:
{# 获取当前文章的完整内容并移除HTML标签 #}
{% archiveDetail articleContent with name="Content" %}
{# 统计纯文本内容中“安企CMS”出现的次数 #}
<p>“安企CMS”在本文纯文本内容中出现了:{{ articleContent|striptags|count:"安企CMS" }} 次。</p>
Further: count keywords of multiple articles
If your goal is to count the number of times a keyword appears in multiple articles on a website, for example on an article list page or a custom aggregation page, thenarchiveListTagwithforLoops will be your helpful assistant.
The following example shows how to count the occurrences of the keyword 'AnQiCMS' in the list of articles:
{% 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 firstarchiveListGot a list of articles and then usedforLoop through each article one by one. Inside the loop, we directly accessitem.Contentto get the article text and pass it throughstriptagsAfter the filter is processed, it is usedcountThe filter performs keyword statistics. The results are assigned tokeywordCounta variable to display on the page
Advanced considerations and practical suggestions
- Case sensitivity:
countThe filter performs an exact match, default 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, but if you perform keyword statistics on the full content of a large number of articles on a page, especially without using cache, it may affect the page loading speed.It is recommended to avoid performing complex real-time keyword statistics on high-traffic pages during peak traffic periods.
- Backend management functionPlease note that the method described here is to calculate the frequency of keywords in real-time in the front-end template, not to generate a permanent statistical report in the background.The AnqiCMS backend provides functions such as "keyword library management" and "site-wide content replacement", which are administrative tools 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 times all keywords in the keyword library appear, you need to first obtain the list of keyword libraries through other tags (such as custom tags, if available), then iterate through the keyword libraries, and perform an
countCounting.
Summary
The AnqiCMS template engine provides extremely high flexibility, even for seemingly complex keyword statistics requirements, which can be achieved through the clever combination of existing tags and filters.In this way, content operators can quickly obtain the keyword distribution information of article content without relying on additional plugins or complex development, providing data support for content optimization.
Frequently Asked Questions (FAQ)
Q1: Can this level of template keyword statistics be directly generated into a report on the Anqi CMS backend? A1:Cannot. The method introduced here is to calculate the number of times keywords appear in real time through template code on the website front page or preview page.The Anqi CMS backend does not have a built-in feature to directly generate keyword statistics reports.If you need background 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, 'AnQiCMS' and 'AnQicms' are considered different keywords.If you need to count without case sensitivity, the built-in filter of Anqi CMS cannot be implemented directly at present, and it may be necessary to customize a Go language template filter to perform a uniform 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 Anqi CMS template,item.ContentorarchiveDetail with name="Content"At the time, if the Markdown editor is enabled on the backend, the system will first render the Markdown content into HTML. Therefore,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.