Understand the Json-LD processing mechanism of AnQi CMS
AnQiCMS provides a very considerate mechanism for handling Json-LD structured data.After you enable the structured data feature in the background, the system will automatically generate a basic Json-LD code at the top of your website page.This code usually includes basic information such as page type, URL, title, description, etc., which is sufficient for most websites to meet daily SEO needs.
However, in some scenarios, you may need to control the content of Json-LD more finely, such as defining more specific Schema properties for specific article types, or supplementing some fields that AnQiCMS does not cover by default.AnQiCMS has fully considered this advanced customization requirement, allowing you to manually intervene, call, and expand these structured data in the template.
In AnQiCMS template call and customize Json-LD
We need to use a dedicated template tag to customize Json-LD structured data in the AnQiCMS template:{% jsonLd %}
Let's understand this process through a simple example. Suppose you want to add a custom author information to a page and specify a particular image as part of Json-LD:
{%- jsonLd -%}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebPage",
"name": "自定义页面名称,覆盖默认",
"description": "这是自定义的页面描述,会覆盖默认的页面描述。",
"author": {
"@type": "Organization",
"name": "AnQiCMS运营团队"
},
"image": [
"https://您的网站域名/static/images/custom-logo.png"
]
}
</script>
{%- endjsonLd -%}
In this code block,{%- jsonLd -%}and{%- endjsonLd -%}Wrapped our custom<script type="application/ld+json">Label. The Json-LD code itself follows the JSON syntax, so key-value pairs, arrays, and objects must be written in strict accordance with the JSON format. It includes such asnameanddescriptionThe field will override the system default generated value, whereasauthorandimagethe field will be merged as new or updated information into the final Json-LD data. Use{%-and-%}You can remove whitespace on both sides of the tag to make the generated HTML more compact.
Combine with actual content for dynamic calls.
The power of Json-LD lies in its ability to dynamically reflect the specific information of the page content.In AnQiCMS, we can combine its rich template tags to introduce dynamic content such as document titles, images, and publication dates into the Json-LD structure, thus building more accurate and semantically meaningful data.
For example, we hope to generate a suitable one for each article on the article detail pageArticleThe Json-LD data type, which includes the title, thumbnail, publication time, author, description, and other information of the article. We can implement it like this:
{%- jsonLd -%}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "{{- archive.Title -}}", {# 调用当前文章标题 #}
"image": [
"{{- archive.Logo -}}" {# 调用当前文章封面首图 #}
],
"datePublished": "{{- stampToDate(archive.CreatedTime, "2006-01-02T15:04:05Z") -}}", {# 格式化文章创建时间为ISO 8601格式 #}
"dateModified": "{{- stampToDate(archive.UpdatedTime, "2006-01-02T15:04:05Z") -}}", {# 格式化文章更新时间 #}
"author": {
"@type": "Person",
"name": "{%- system with name='SiteName' -%}" {# 调用后台设置的网站名称作为作者 #}
},
"publisher": {
"@type": "Organization",
"name": "{%- system with name='SiteName' -%}", {# 调用后台设置的网站名称作为发布者 #}
"logo": {
"@type": "ImageObject",
"url": "{%- system with name='SiteLogo' -%}" {# 调用后台设置的网站Logo作为发布者Logo #}
}
},
"description": "{{- archive.Description | truncatechars:200 -}}", {# 调用文章简介,并截取前200字 #}
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "{{- archive.Link -}}" {# 调用文章链接 #}
}
}
</script>
{%- endjsonLd -%}
In this code block:
- We used
archive.Title/archive.Logo/archive.Descriptionandarchive.LinkDirectly get the title, cover image, description, and link of the current page. stampToDateThe filter will convert Unix timestamp format toarchive.CreatedTimeandarchive.UpdatedTimeConverted to the ISO 8601 format required for Json-LD"2006-01-02T15:04:05Z"A special template used in Go language for formatting time