As an experienced website operation expert, I fully understand that in the context of the increasing importance of content marketing and search engine optimization (SEO) at present, it is crucial to effectively convey the core information of the page content to search engines.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, which provides us with great convenience with its efficient and customizable features.Today, let's delve deeply into a detail often mentioned in SEO but often overlooked: how to dynamically fill Json-LD in the AnQiCMS article page withauthoranddatePublishedfield.
Json-LD: The 'identity card' for search engines to understand content
In the context of search engines, Json-LD (JavaScript Object Notation for Linked Data) plays the role of a content 'ID card'.It is a structured data markup language that can help search engines clearly understand the meaning, type, and relationships between web content.Using Json-LD correctly not only enhances the depth of search engines' understanding of page content, but also gives the opportunity to obtain "rich text snippets" (Rich Snippets) in search results, such as the publication date of the article, author information, and even ratings. These can significantly increase click-through rates and bring more high-quality traffic to the website.
For article pages,author(author), anddatePublished(Publish date) these fields are particularly important.authorFields help establish content creators' professionalism, authority, and credibility (core elements of the E-A-T principle), standing out among numerous pieces of information.datePublishedIt can clearly tell the search engine and users the publication time of the content, which is crucial for news, technical articles, and other time-sensitive content. It can help the search engine judge the freshness and relevance of the content.
Why is dynamic filling needed?
Imagine if your website had hundreds or even thousands of articles, each requiring manual editing of Json-LD code, which would not only be inefficient but also prone to errors.When the author changes or the publication date is adjusted, manual maintenance is a nightmare.Therefore, dynamically populating these fields is an inevitable choice in website operation, which allows your website to automatically generate and update structured data when publishing new content, greatly enhancing operational efficiency.
Anqi CMS template engine provides powerful dynamic data calling capabilities, combined with its specially designed for structured datajsonLdtags, making dynamic filling possibleauthoranddatePublishedIt becomes simple and efficient.
How does AnQiCMS dynamically fill in?datePublishedfield
In AnQiCMS, the publishing and updating time of each article is stored in the database and accessed througharchiveDetailTags are provided for template use. They need to be filled in.datePublishedFields, we need to pay attention toCreatedTime(creation time) andUpdatedTime(Update time). These two fields are usually stored in the form of timestamps in AnQiCMS.
Json-LD has strict requirements for date formats, usually recommending the use of the ISO 8601 standard, for example2023-10-26T15:00:00+08:00. Anqi CMS'sstampToDateThe (timestamp formatting) feature can exactly help us easily achieve this conversion.
We can use it in the template of the article detail page (usually){模型table}/detail.htmlin the}archiveDetailTag to get the current article'sCreatedTimeorUpdatedTimeThen proceedstampToDateFormat it as a date string required for Json-LD.
For example, if you want to use thedatePublished:
{%- archiveDetail articleCreatedTime with name="CreatedTime" %}
{%- set formattedDate = stampToDate(articleCreatedTime, "2006-01-02T15:04:05-07:00") %}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"datePublished": "{{ formattedDate }}"
// ... 其他Json-LD字段
}
</script>
In the code above:
- We first use
{%- archiveDetail articleCreatedTime with name="CreatedTime" %}Get the current creation timestamp of the article and assign it toarticleCreatedTimeVariable. - Then,
{%- set formattedDate = stampToDate(articleCreatedTime, "2006-01-02T15:04:05-07:00") %}Format the timestamp. It is important to note that the time formatting string in the Go language is very unique, it is notY-m-d H:i:ssuch placeholders, but with2006-01-02T15:04:05-07:00This specific date-time string is used as a reference template to define the format. - Finally, in
scriptthe tag,"datePublished": "{{ formattedDate }}"the dynamic filling is completed.
If you want to prioritize the update time of the article, you can replace it withname="UpdatedTime". Usually, if the content of the article is substantially updated, usingUpdatedTimewill better reflect the latest status of the content.
How does AnQiCMS dynamically fill in?authorfield
authorThe dynamic filling of the field may require a little extra setup because the default article model of AnQi CMS may not have a direct field corresponding to 'author name'. But don't worry, AnQi CMS has the 'flexible content model' feature (AnQiCMS 项目优势.mdIt is mentioned) it is born for this.
Step one: Create a custom author field.
First, you need to add a custom field for storing author information in the AnQi CMS backend for your article model.
- Log in to the Anqi CMS backend and find "Content Management" under it, then locate the "Content Model".
- Edit the article model you are using (such as "Article Model" or a custom model you created).
- In the model editing interface, click 'Customize content model fields' to add a new field.
- Parameter name (display name)For example, 'Article Author'
- Field call (template call name)For example
articleAuthor(please use english lowercase letters) - Field typeselect 'single line text'
- you can set whether it is required, default value, and so on as needed.
- save model configuration.
After completing this step, you will be able to fill in the custom field "Article Author" when publishing or editing articles.
Step two: Dynamically fill in the author information in the template.
With custom fields, we can usearchiveDetailtags to get the author information.
Json-LD'sauthorA field can be a string (author name), or a more complex object, such asPerson(personal) orOrganization(Organization), provide more details such as the author's URL, social media links, etc. Here we takePersontype for example.
{%- archiveDetail articleAuthorName with name="articleAuthor" %}
{%- archiveDetail articleAuthorLink with name="articleAuthorLink" %} {# 假设您也创建了作者链接的自定义字段 #}
{%- if not articleAuthorName %}
{%- set articleAuthorName = "您的网站名称" %} {# 如果作者字段为空,提供一个默认值 #}
{%- endif %}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
// ... 其他Json-LD字段
"author": {
"@type": "Person",
"name": "{{ articleAuthorName }}",
{%- if articleAuthorLink %}
"url": "{{ articleAuthorLink }}"
{%- endif %}
}
}
</script>
In the above code:
{%- archiveDetail articleAuthorName with name="articleAuthor" %}to retrieve custom fieldsarticleAuthorthe value.{%- archiveDetail articleAuthorLink with name="articleAuthorLink" %}Retrieve the possibly existing custom author link field.- We have also added a
ifDetermine ifarticleAuthorNameIf it is empty, set it to a default value to avoid missing Json-LD structure. "author": { ... }Partially constructed aPersonA type of author object that includes the author's name and (if available) the author's URL.
Complete example code
Now, let's transformdatePublishedandauthorIntegrate the dynamic filling into a complete Json-LD structure and it is recommended to place it on the article detail page template's<head>Inside the tag:
”`twig {# Get the current article object to call its properties uniformly #} {%- archiveDetail currentArchive %}
{%- if currentArchive.Id %} {# Ensure the current page is the article detail page #}
{# 格式化发布时间 #}
{%- set publishedDate = stampToDate(currentArchive.CreatedTime, "2006-01-02T15:04:05-07:00") %}
{# 格式化更新时间,如果文章有更新,优先使用更新时间 #}
{%- set modifiedDate = stampToDate(currentArchive.UpdatedTime, "2006-01-02T15:04:05-07:00") %}
{# 获取自定义作者信息,如果为空则使用网站名称作为默认作者 #}
{%- archiveDetail articleAuthorName with name="articleAuthor" id=currentArchive.Id %}
{%- if not articleAuthorName %}{%- set articleAuthorName = system.SiteName -%}{%- endif %}