Hello everyone, website operators and partners!As an expert who has been deeply involved in website operations for many years, I know that how to make the website content stand out and be better understood and displayed by search engines is a crucial part of our daily work.AnQiCMS, with its powerful functions and flexible customization, has provided us with many conveniences.forLoop tags, dynamically generate list or array data, especially for structured content like comment lists, thus winning more display opportunities for your website.

Json-LD: Let search engines understand your content

In AnQiCMS management and content output, we often mention structured data.And Json-LD (JSON for Linking Data) is a structured data format recommended by major search engines like Google.It allows us to embed information from web pages in a structured way into HTML, so that search engines can not only "see" your content but also "understand" the meaning and relationships of the content.For example, a product page, in addition to displaying the product name and price, Json-LD can also tell the search engine that this is a 'product', its 'brand', 'model', 'user reviews', and other specific information. This is indispensable for enhancing the rich media display of search results (Rich Snippets), such as star ratings, price ranges, and so on.

When our content includes dynamically generated list data, such as user comments, related article lists, product specifications, etc., traditional static Json-LD seems inadequate. At this point, the powerful template engine of AnQiCMS, especially itsforLoop tags, and they become the power tool for us to dynamically build Json-LD.

forLoop tags: The core driving force of dynamically building Json-LD.

AnQiCMS's template engine adopts a syntax style similar to Django, which makesforthe use of loops very intuitive and powerful.{% for item in items %}...{% endfor %}This structure can help us easily traverse any array or list data and process eachitemPerform the operation.In the context of Json-LD, this means that we can construct data items in Json-LD one by one based on the comments, articles, and other data obtained from the background.

It is worth mentioning that AnQiCMS also provides{% jsonLd %}A tag that acts like a smart container, allowing us to write Json-LD code within it, and ultimately have the system correctly insert it into the appropriate position on the HTML page (usually<head>or<body>Inside the tag). What's even better is that if we are{% jsonLd %}The field defined internally conflicts with the Json-LD field generated by the system, the part we customize will take precedence, which gives us great flexibility to finely control the output of structured data.

Practice: Generating a comment list in Json-LD

Now, let's take a common scenario as an example - providing structured data of user comments for search engines on an article detail page. We will combine AnQiCMS'scommentListTag to obtain comment data and utilizeforLoop to generate one in Json-LDreviewarray.

Firstly, we need to operate in the article detail page template (such asarchive/detail.html)

The first step: obtain comment data

We will use the built-in AnQiCMScommentListTag to retrieve all approved comments of the current article. This tag can filter comments based on article ID and allows us to control the number of comments displayed.

{# 假设我们已经在模板中获取了当前文章的详情,并将其赋值给 archive 变量 #}
{# 获取当前文章的已审核评论,限制数量,可根据需要调整 #}
{% commentList comments with archiveId=archive.Id type="list" limit="10" %}

here,commentsThe variable now contains all the comments data of the article, it is an array of comment objects. Each comment object may containId/UserName/Content/CreatedTime/Status/VoteCountfields.

Step two: Build the Json-LD container and basic structure

Next, we use{% jsonLd %}Wrap our Json-LD code with tags and define the basic article or product structure, for exampleArticleorProductand reserve a position for the comment list. We usually userevieworaggregateRatingto represent comment information.

{% jsonLd %}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Article", {# 或者 "Product", 根据您的页面类型而定 #}
  "mainEntityOfPage": {
    "@type": "WebPage",
    "lastReviewed": "{{ stampToDate(archive.UpdatedTime, "2006-01-02T15:04:05Z07:00") }}", {# 页面最后更新时间 #}
    "url": "{{ archive.Link }}"
  },
  "headline": "{{ archive.Title }}",
  "description": "{{ archive.Description|truncatechars:150 }}",
  "image": [
    "{{ archive.Logo }}" {# 可以是多个图片URL的数组 #}
  ],
  {# 这里将是评论列表的插入位置 #}
  "review": [
    {# 我们的for循环会在这里填充评论数据 #}
  ]
}
</script>
{% endjsonLd %}

Third step: CombineforLoop to fill in the comment data

Now, we willforNested into Json-LD'sreviewarray. We need to traversecommentsthe array, and map each comment's fields to the structure required by Json-LDReviewin the type.

”`twig {% Ld %}