As an experienced website operations expert, I am well aware of the importance of structured data, especially Json-LD, in the context of increasingly refined search engine optimization (SEO) and its impact on the visibility and rich presentation of website content.AnQiCMS (AnQiCMS) is a highly efficient and flexible content management system, which naturally also provides us with powerful tools to achieve this goal.

Today, let's delve deeply into a problem that often occurs in actual operation:Json-LDDoes the custom tag support fromdiyCustom content tag to retrieve data?In short, the answer is affirmative.The design philosophy of AnQi CMS is flexibility and customization, its template engine allows for nested use of tags, thereby achieving more complex and dynamic data integration.

The importance of Json-LD and the flexible foundation of Anqi CMS

Before we delve into the details, let's review the value of Json-LD.Json-LD is a lightweight data interoperability method based on the JSON format, which helps search engines better understand the meaning of web content, such as the author of an article, publication date, product price, review ratings, and more.By embedding Json-LD code in a webpage, a website can obtain richer search result displays (Rich Snippets), thereby increasing click-through rates and traffic.

AnQi CMS is a CMS based on Go language development, with its powerful content model, customizable fields, and Django-like template engine syntax, which provides us with highly customized content publishing capabilities. Whether it is global system settings (such as company contact information), customized properties of specific content models (such as articles or products), or general customizable content (viadiyLabel management), AnQiCMS can easily call its built-in template tags.This data call flexibility is the key to our implementation of dynamic Json-LD structured data.

ExploreJson-LDCustom tag

Anqi CMS built-injsonLdCustom tags allow us to directly define Json-LD code blocks in templates. The basic usage is as shown in the document:

{% jsonLd %}
<script type="application/ld+json">
{
	"author": "AnQiCMS",
	"image": [
		"https://en.anqicms.com/anqicms.png"
	]
}
</script>
{% endjsonLd %}

The cleverness of this tag lies in the fact that it is not simply outputting static JSON-LD code. The document mentions, 'You just need to wrap the content in'}]{...}Enter the custom field you need to write, Anqi CMS will automatically merge and process the data, and if the field you define conflicts with the default field, the default field will be overridden.This sentence implies the dynamic nature of its internal processing, that is, it can parse and merge the content we provide.

The strength of the AnQiCMS template engine lies in,it supports nested use of tags. This means that we can completely usejsonLdwithin the tag,<script>In the block, continue to use other AnQiCMS template tags to dynamically retrieve data.

FromdiyCustom content tags to retrieve data and integrateJson-LD

diyCustom content tags are a very useful feature in AnQiCMS, allowing us to flexibly add global or site-specific custom parameters in the background, such as customer service phone numbers, company unified email addresses, and specific announcement texts, etc.This information often needs to appear in multiple places on the website, including in Json-LD.

Suppose we pass through the backgrounddiyCustom content set a namedCustomerServicePhoneThe parameter is used to store the company's customer service phone number. If we want to mark the website as a "Organization" type in Json-LD and include this customer service phone number, we can do it like this:

{% jsonLd %}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Organization",
  "name": "{% system with name='SiteName' %}", {# 调用系统设置中的网站名称 #}
  "telephone": "{% diy with name='CustomerServicePhone' %}", {# 从diy自定义内容中获取客服电话 #}
  "url": "{% system with name='BaseUrl' %}" {# 调用系统设置中的网站基础URL #}
}
</script>
{% endjsonLd %}

In this example,{% diy with name='CustomerServicePhone' %}The tag is ingeniously embedded in the JSON structure of Json-LD. When AnQiCMS renders the page, it will first parsediyLabel, obtain the customer service phone number configured on the backend, and then insert the phone number as a string intotelephonethe field value, finally generating the complete Json-LD code.

Content model custom field withJson-LDthe combination of

except the globaldiyLabel, we often use custom fields in the content model.For example, an article may have a custom "author bio" field, and a product may have custom attributes such as "production location" or "shelf life".This information is very important structured data in Json-LD.

assuming we have an 'article model' that includes a custom fieldarticleAuthorTo store the name of the article author, and we want to reflect this in the Json-LD of the article detail page:

{# 确保在文章详情页面的上下文中使用,以获取当前文档的数据 #}
{% archiveDetail currentArticle with name="Id" %} {# 确保我们处于文档详情页的正确上下文 #}
{% jsonLd %}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article",
  "headline": "{% archiveDetail with name='Title' %}",
  "description": "{% archiveDetail with name='Description' %}",
  "image": "{% archiveDetail with name='Logo' %}",
  "datePublished": "{{ stampToDate(archive.CreatedTime, '2006-01-02T15:04:05-07:00') }}",
  "author": {
    "@type": "Person",
    "name": "{% archiveDetail with name='articleAuthor' %}" {# 假设articleAuthor是自定义字段 #}
  },
  "publisher": {
    "@type": "Organization",
    "name": "{% system with name='SiteName' %}",
    "logo": {
      "@type": "ImageObject",
      "url": "{% system with name='SiteLogo' %}"
    }
  }
}
</script>
{% endjsonLd %}

Here, {% archiveDetail with name='articleAuthor' %}Extract the author's name directly from the custom fields of the current article. The same principle applies to other custom fields, whether they are text, numbers, links, or image URLs.

If the custom field is multiline text or rich text and may contain HTML tags, in order to meet the Json-LD pure text value requirements, we may need to use a filter to process it, for examplestriptagsRemove HTML tags, ortruncatecharsTruncate the long text to avoid Json-LD parsing errors:

”`twig {# Assume the custom field "product highlights" (productHighlights) is rich text content #} {% archiveDetail productHighlights with name=“productHighlights” %} {% Ld %}