As an experienced website operations expert, I am well aware that in the increasingly important context of content marketing and search engine optimization (SEO), it is crucial to effectively convey the core information of the page content to search engines.English CMS (EnglishCMS) as an enterprise-level content management system developed based on Go language, with its high efficiency and customizable features, provides us with great convenience.authoranddatePublishedfield.

Json-LD: The 'ID 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 helps search engines clearly understand the meaning, type, and relationships between the content of web pages.Correctly using Json-LD can not only enhance the depth of search engines' understanding of page content but also have the opportunity to obtain 'rich text summary' (Rich Snippets) in search results, such as the publication date of the article, author information, and even ratings, all of which can significantly improve click-through rates and bring more high-quality traffic to the website.

For article pages,author(Author) anddatePublished(Publish date) these two fields are particularly important.authorThe field helps to establish the professionalism, authority, and credibility of content creators (a core element 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 content with strong timeliness such as news, technical articles, etc. 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 thousands of articles, and you had to manually edit the Json-LD code for each one. This would be inefficient and 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 releasing new content, greatly enhancing operational efficiency.

The template engine of Anqi CMS provides powerful dynamic data call capabilities, combined with its specifically designed for structured datajsonLdtags, making dynamic fillingauthoranddatePublishedBecomes simple and efficient.

How AnQiCMS dynamically fills indatePublishedField

In AnQiCMS, the publishing and updating time of each article is stored in the database and is displayed througharchiveDetailThe label is provided for template use. It needs to be filled in.datePublishedWe need to pay attention to the field.CreatedTime(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. The files of AnQi CMS arestampToDateThe function of (formatted timestamp), just helps us easily achieve this conversion.

We can use the template in the article detail page (usually){模型table}/detail.html), to use thearchiveDetailtag to get the current article'sCreatedTimeorUpdatedTimeand then throughstampToDateFormat it into a date string required for JSON-LD.

For example, if you want to use the creation time of the article asdatePublished:

{%- 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 above code:

  • We first use{%- archiveDetail articleCreatedTime with name="CreatedTime" %}Get the creation timestamp of the current article and assign it toarticleCreatedTimea variable.
  • Next,{%- set formattedDate = stampToDate(articleCreatedTime, "2006-01-02T15:04:05-07:00") %}Translate timestamp. It is important to note that the time formatting string in Go language is very unique, it is notY-m-d H:i:ssuch placeholders, but with2006-01-02T15:04:05-07:00This specific date and time string is used as a reference template to define the format.
  • Finally,scriptin the 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". Generally, if the content of the article is significantly updated, usingUpdatedTimewill better reflect the latest status of the content.

How AnQiCMS dynamically fills inauthorField

authorThe dynamic filling of fields may require a little extra setup, because the default article model of Anqi CMS may not have a direct field corresponding to "author nameAnQiCMS 项目优势.mdauto mentioned) is for this reason.

Step 1: Create a custom author field

Firstly, you need to add a custom field for storing author information in the article model on the Anqi CMS backend.

  1. Enter the AnQi CMS backend, find the "Content Management" section and then "Content Model".
  2. Edit the article model you are using (such as "Article Model" or a custom model you created).
  3. In the model editing interface, click 'Customize field of content model' to add a new field.
    • Parameter name (display name)'} ]For example, 'Article Author'
    • Field invocation (template invocation name): For examplearticleAuthor(please use english lowercase letters)
    • field typeselect "single line text"
    • you can set whether it is required, default value, etc. as needed.
  4. save model configuration.

After completing this step, you can 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 use it just like getting information about other articles.archiveDetailTags to get author information.

Json-LD'sauthorThe field can be a string (author name), or a more complex object,Person(personal) orOrganization(Organization),provide more details, such as the author's URL, social media links, etc. Here we takePersonType as an 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" %}Then fetch the custom author link field if it exists.
  • We also added aifdetermine, ifarticleAuthorNameis empty, then set a default value to avoid missing Json-LD structure.
  • "author": { ... }Partly constructed anPersonauthor object of type, which includes the author's name and (if available) the author's URL.

Complete example code

Now, let's godatePublishedandauthorThe dynamic filling integration into a complete Json-LD structure, and it is recommended to place it in the article detail page template.<head>Inside the tag:

`twig {# 获取当前文章对象,以便统一调用其属性 #} {%- 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 %}