When using AnQiCMS for website content operations, we often hope that the URL addresses in articles, product descriptions, or other text content can be automatically recognized and converted into clickable links. This not only improves user experience but also helps in the dissemination of content and search engine friendliness.AnQiCMS as a content management system focusing on efficiency and SEO optimization naturally considers this requirement and provides a very convenient implementation method.This is mainly due to its flexible template engine and built-in filter functions.
To implement the automatic resolution of URL addresses in article content, we do not need to manually edit the published articles, nor do we need to install any additional plugins. All it takes is a slight adjustment at the template level. The core lies in using the AnQiCMS template engine provided byurlizeandsafeThese two filters.
urlizeFilters are specifically designed to identify URLs in plain text content. It can intelligently recognize various URL formats in the content of articles, such ashttp://www.example.com/https://www.example.comevenwww.example.comFormatted URLs and email addresses are then automatically wrapped in HTML tags<a>These tags make them clickable links. According to the AnQiCMS documentation,urlizeWhen converting the link, it will also automatically increaserel="nofollow"Property, this helps to avoid SEO weight loss and tells search engines that these external links should not be tracked, which is a good guarantee for the SEO health of the website.
AndsafeThe filter plays the role of a guardian. BecauseurlizeThe filter generates HTML code, in order to ensure that this code can be correctly parsed by the browser rather than displayed as plain text, we must use it in conjunction withsafefilter.safeThe filter informs the template engine that the output content is safe HTML and does not require escaping processing.
In practice, we usually apply these two filters to the article content field in the template file used to display article details. For example, if your article content is stored inarchive.ContentField, and pass througharchiveDetailTag to retrieve and display, then you can operate like this:
{# 假设您正在展示文章详情,文章内容通常由archive.Content字段提供 #}
{% archiveDetail articleContent with name="Content" %}
<div class="article-body">
{{ articleContent|urlize|safe }}
</div>
{% endarchiveDetail %}
In this code block,{{ articleContent|urlize|safe }}The purpose is: first,articleContentPure text URL in it will beurlizeIdentified and converted by the filter<a>Tagged HTML links; then,safeFilter ensures that the generated HTML code is correctly rendered by the browser.
In addition, when an article contains some particularly long URL addresses, if they are displayed in full, it may affect the aesthetics and layout of the page, AnQiCMS also providesurlizetruncFilter. This filter allows us to set a truncation length while converting links, and the excess part will be indicated with an ellipsis....Replace, keep the page tidy. For example, if you want the link text to display a maximum of 15 characters, you can use it like this:
{# 使用urlizetrunc过滤器截断长链接文本 #}
{% archiveDetail articleContent with name="Content" %}
<div class="article-body">
{{ articleContent|urlizetrunc:15|safe }}
</div>
{% endarchiveDetail %}
This automatic parsing feature is not only applicable to article content (ContentField), any text field that may contain a URL, such as an article summaryDescriptionCustom text fields can also be applied in a similar mannerurlizeorurlizetruncA filter to automatically convert URLs.
It should be noted that,urlizeandurlizetruncThe filter mainly targets plain text URLs that have not been formatted in the article. If you have manually inserted links using the rich text editor's link button while editing the article, or used Markdown syntax (such as[点击这里](https://en.anqicms.com)) to create links, then these links themselves already have clickable, no need to go throughurlizeorurlizetruncFilter processing. The "anchor text" feature and the "full site content replacement" feature provided by AnQiCMS are designed for on-site SEO and batch content management, focusing on global replacement and optimization of specific keywords or links, andurlizeThe filter handles the instant conversion of plain text URLs differently on the article display layer.
Through this simple and powerful way, AnQiCMS allows website operators to easily convert URLs in article content, significantly improving the user experience and avoiding the trouble of manually copying and pasting links.At the same time, this also indirectly promotes the accessibility of content and the understanding of resources mentioned by search engines, which has a positive impact on the overall performance of the website.
Frequently Asked Questions (FAQ)
- Q: If I have manually added links in the article editor,
urlizeWill the filter still process them? Answer:No.urlizeThe filter mainly targets URLs in plain text format. If you have manually added links to the text using a rich text editor or Markdown syntax, these links are already in HTML<a>tag,urlizeThe filter will skip them, not repeating the processing or changing their structure. - Question: Does the automatically parsed link contain
rel="nofollow"attributes, will this affect SEO? Answer:Yes, according to the filter description of AnQiCMS,urlizethe filter will automatically addrel="nofollow"Property. This is generally considered a beneficial practice for SEO, as it informs search engines that these external links should not pass weight, helping to concentrate the SEO weight of your website on internal links and avoid unnecessary external weight loss. - Can I control to automatically parse only some URLs and not others?
Answer:
urlizeThe filter is processed at the template level, by default it converts all matching plain text URLs. If you need to be