【问题标题】:Use liquid object in json scheme shopify在 json 模式 shopify 中使用液体对象
【发布时间】:2018-10-04 01:08:40
【问题描述】:

是否有任何选项可以在 json 方案标签中使用液体对象? 例如,我有一个 product-template.liquid 文件,其中包含以下架构:

{% schema %}
{
  "name": "Product pages",
  "settings": [
    {
      "type": "text",
      "id": "text_custom_1",
      "label": "Custom text"
    },
  ]
}
{% endschema %}

我想要实现的是获取产品的动态 id 作为自定义字段的 id,如下所示:

{% schema %}
{
  "name": "Product pages",
  "settings": [
    {
      "type": "text",
      "id": "text_custom_1_{{%product.id%}}",
      "label": "Custom text"
    },
  ]
}
{% endschema %}

但这会产生 json 语法错误。以上在某种程度上可能吗?

【问题讨论】:

    标签: json schema shopify liquid


    【解决方案1】:

    有一种方法可以编写您自己的 LD-JSON 标记,从您的 SHopify 商店选项等中提取数据。

    请看一下这个例子,将它添加到 DOM 底部的 Liquid Template 位置,之前:

    {% comment %}
      Article structured data
      https://developers.google.com/search/docs/data-types/articles
    {% endcomment %}
    
    {% if template contains 'article' %}
      {%- capture article_description  -%}
        {%- if article.excerpt != blank -%}
          {{ article.excerpt | strip_html }}
        {%- else -%}
          {{ article.content | truncatewords: 100 | strip_html }}
        {%- endif -%}
      {%- endcapture -%}
    
      <script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "BlogPosting",
          "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "{{ shop.url }}{{ article.url }}"
          },
          "headline": {{ article.title | json }},
          {% if article.image %}
            "image": {
              "@type": "ImageObject",
              "url": {{ article.image | img_url: '800x800' | prepend: 'https:' | json }},
              "height": 800,
              "width": 800
            },
          {% endif %}
          "datePublished": {{ article.published_at | date: '%Y-%m-%dT%H:%M:%S%z' | json}},
          "dateModified": {{ article.published_at | date: '%Y-%m-%dT%H:%M:%S%z' | json}},
          "author": {
            "@type": "Person",
            "name": {{ article.author | json }}
          },
          "publisher": {
            "@type": "Organization",
            "name": {{ shop.name | json }},
            "logo": {
              "@type": "ImageObject",
              "url": {{ article.user.image | img_url: '200x200' | prepend: 'https:' | json }}
            }
          },
          "description": {{ article_description | json }}
        }
      </script>
    {% endif %}
    
    {% comment %}
      Breadcrumbs
      https://developers.google.com/search/docs/data-types/breadcrumbs
    {% endcomment %}
    
    {%- if current_tags -%}
      {%- assign tag_names = current_tags | join: ', ' %}
      {% comment %}Converting an array to a handle converts it to a string{% endcomment %}
      {%- capture tag_handles -%}
        {%- for tag in current_tags -%}
          {{- tag | handle | append: '|' -}}
        {%- endfor -%}
      {%- endcapture -%}
    {%- endif -%}
    
    {% if collection.url == blank %}
      {% assign collection_url = '/collections/all' %}
    {% else %}
      {% assign collection_url = collection.url %}
    {% endif %}
    
    {% if template.name == 'product' 
        or template.name == 'list-collections' 
        or template.name == 'collection' 
        or template.name == 'blog' 
        or template.name == 'article' 
        or template.name == 'search' 
        or template.name == 'cart' 
        or template.name == 'page'
    %}
    
      <script type="application/ld+json">
        {
          "@context": "http://schema.org",
          "@type": "BreadcrumbList",
          "itemListElement": [
          {% case template.name %}
            {% when 'product' %}
              {% if product.collections.size >= 1 %}
                {
                  "@type": "ListItem",
                  "position": 1,
                  "item": {
                    "@id": "{{ shop.url }}/collections",
                    "name": {{ 'structured_data.breadcrumbs.collections' | t | json }}
                  }
                },
                {
                  "@type": "ListItem",
                  "position": 2,
                  "item": {
                    "@id": "{{ shop.url }}/collections/{{ product.collections.first.handle }}",
                    "name": {{ product.collections.first.title | json }}
                  }
                },
              {% else %}
                {
                  "@type": "ListItem",
                  "position": 1,
                  "item": {
                    "@id": "{{ shop.url }}/products",
                    "name": {{ 'structured_data.breadcrumbs.products' | t | json }}
                  }
                },
              {% endif %}
              {
                "@type": "ListItem",
                "position": {% if product.collections.size >= 1 %}3{% else %}2{% endif %},
                "item": {
                  "@id": "{{ shop.url }}{{ product.url | within: collection }}",
                  "name": {{ product.title | json }}
                }
              }
            {% when 'list-collections' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ shop.url }}/collections",
                  "name": {{ 'structured_data.breadcrumbs.collections' | t | json }}
                }
              }
              {% if current_page != 1 %}
                ,
                {
                  "@type": "ListItem",
                  "position": 2,
                  "item": {
                    "@id": "{{ shop.url }}/collections",
                    "name": {{ 'structured_data.breadcrumbs.page' | t: page: current_page | json }}
                  }
                }
              {% endif %}
            {% when 'collection' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ shop.url }}/collections",
                  "name": {{ 'structured_data.breadcrumbs.collections' | t | json }}
                }
              },
              {
                "@type": "ListItem",
                "position": 2,
                "item": {
                  "@id": "{{ shop.url }}{{ collection_url }}",
                  "name": {{ collection.title | json }}
                }
              }
              {% if current_tags %}
                ,
                {
                  "@type": "ListItem",
                  "position": 3,
                  "item": {
                    "@id": "{{ shop.url }}{{ collection_url }}/{{- tag_handles | split: '|' | join: '+' -}}",
                    "name": {{ 'structured_data.breadcrumbs.tags_html' | t: tags: tag_names | json }}
                  }
                }
              {% endif %}
              {% if current_page != 1 %}
                {% if current_tags %}
                  {% assign position = 4 %}
                {% else %}
                  {% assign position = 3 %}
                {% endif %}
                ,
                {
                  "@type": "ListItem",
                  "position": {{ position }},
                  "item": {
                    "@id": "{{ shop.url }}/collections?page={{ current_page }}",
                    "name": {{ 'structured_data.breadcrumbs.page' | t: page: current_page | json }}
                  }
                }
              {% endif %}
            {% when 'blog' or 'article' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ shop.url }}{{ blog.url }}",
                  "name": {{ blog.title | json }}
                }
              }
              {% if template.name == 'article' %}
                ,
                {
                  "@type": "ListItem",
                  "position": 2,
                  "item": {
                    "@id": "{{ shop.url }}{{ article.url }}",
                    "name": {{ article.title | json }}
                  }
                }
              {% elsif current_tags %}
                ,
                {
                  "@type": "ListItem",
                  "position": 2,
                  "item": {
                    "@id": "{{ shop.url }}{{ blog.url }}/tagged/{{- tag_handles | split: '|' | join: '+' -}}",
                    "name": {{ 'structured_data.breadcrumbs.tags_html' | t: tags: tag_names | json }}
                  }
                }
              {% endif %}
              {% if current_page != 1 %}
                {% if current_tags %}
                  {% assign position = 3 %}
                  {%- capture url -%}
                    {{ shop.url }}{{ blog.url }}/tagged/{{ tag_handles | split: '|' | join: '+' }}?page={{ current_page }}
                  {%- endcapture -%}
                {% else %}
                  {% assign position = 2 %}
                    {%- capture url -%}
                    {{ shop.url }}{{ blog.url }}?page={{ current_page }}
                  {%- endcapture -%}
                {% endif %}
                ,
                {
                  "@type": "ListItem",
                  "position": {{ position }},
                  "item": {
                    "@id": "{{ url }}",
                    "name": {{ 'structured_data.breadcrumbs.page' | t: page: current_page | json }}
                  }
                }
              {% endif %}
            {% when 'search' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ shop.url }}/search",
                  "name": {{ 'structured_data.breadcrumbs.search' | t | json }}
                }
              }
              {% if search.performed %}
                {
                  "@type": "ListItem",
                  "position": 2,
                  "item": {
                    "@id": "{{ shop.url }}/search?q={{ search.terms }}",
                    "name": {{ search.terms | json }}
                  }
                }
                {% if current_page != 1 %}
                  {
                    "@type": "ListItem",
                    "position": 3,
                    "item": {
                      "@id": "{{ shop.url }}/search?page={{ current_page }}&q={{ search.terms }}",
                      "name": {{ 'structured_data.breadcrumbs.page' | t: page: current_page | json }}
                    }
                  }
                {% endif %}
              {% endif %}
            {% when 'cart' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ shop.url }}/cart",
                  "name": {{ 'structured_data.breadcrumbs.cart' | t | json }}
                }
              }
            {% when 'page' %}
              {
                "@type": "ListItem",
                "position": 1,
                "item": {
                  "@id": "{{ page.url }}",
                  "name": {{ page.title | json }}
                }
              }
            {% else %}
            {% endcase %}
          ]
        }
      </script>
    {% endif %}
    
    {% comment %}
      Product structured data
      https://developers.google.com/search/docs/data-types/products
    {% endcomment %}
    
    {% if template contains 'product' %}
      {% assign selected_variant = product.selected_or_first_available_variant | default: product.variants.first %}
      {% assign product_image = selected_variant.featured_image | default: product.featured_image %}
      {%- capture product_name -%}
        {{ product.title }}
        {%- if selected_variant.title != 'Default Title' and selected_variant.option1 == 'Default Title' -%}
          - {{ selected_variant.title }}
        {%- endif -%}
      {%- endcapture -%}
    
      {%- assign now = 'now' | date: '%Y-%m-%d' | split: '-' -%}
      {% capture year_from_now %}{{ now[0] | plus: 1 }}-{{ now[1] }}-{{ now[2] | at_most: 28 }}{% endcapture %}
    
      <script type="application/ld+json">
      {
        "@context": "http://schema.org/",
        "@type": "Product",
        "name": {{ product_name | strip_newlines | json }},
        "image": {{ product_image | img_url: '1024x1024' | prepend: 'https:' | json }},
        {% if product.description != blank %}
          "description": {{ product.description | json }},
        {% endif %}
        {% if product.vendor %}
          "brand": {
            "@type": "Thing",
            "name": {{ product.vendor | json }}
          },
        {% endif %}
        {% if selected_variant.sku != blank %}
          "sku": {{ selected_variant.sku | json }},
        {% endif %}
        {% if selected_variant.barcode != blank %}
          "mpn": {{ selected_variant.barcode | json }},
        {% endif %}
        "offers": {
          "@type": "Offer",
          "priceCurrency": {{ cart.currency.iso_code | json }},
          "price": {{ selected_variant.price | divided_by: 100.0 | json }},
          "availability": "http://schema.org/{% if selected_variant.available %}InStock{% else %}OutOfStock{% endif %}",
          "url": "{{ shop.url }}{{ selected_variant.url }}",
          "seller": {
            "@type": "Organization",
            "name": {{ shop.name | json }}
          },
          "priceValidUntil": {{ year_from_now | json }}
        }
      }
      </script>
    {% endif %}
    
    {% comment %}
      Store information
      https://developers.google.com/search/docs/data-types/sitename
    {% endcomment %}
    
    <script type="application/ld+json">
      {
        "@context": "http://schema.org",
        "@type": "WebSite",
        "name": "{{ shop.name }}",
        "url": "{{ shop.url }}"
      }
    </script>
    
    <script type="application/ld+json">
    {
      "@type": "Organization",
      "@context": "http://schema.org/",
      "name": "{{shop.name}}",
      "logo": {
          "@type": "imageObject",
          "url": "https:{{ 'logo.png' | asset_img_url: '300x240' | split: '?' | first }}",
          "height": "300px",
          "width": "240px"
       },
      "url": "{{ shop.url }}"
    }
    </script>
    

    最初在这里看到:https://gist.github.com/wmulligan87/1368b434a880b2517fc2152989fbd2df

    这应该可以满足您的所有需求,希望有人会发现这对您有所帮助,因为这解决了很多问题。

    【讨论】:

      【解决方案2】:

      不,没有在架构对象内使用任何流动代码的选项。

      如果您想要任何动态内容,最好使用块。

      【讨论】:

      • 我不知道你为什么这么说,因为这很容易通过 Liquid 模板实现
      • @dExIT 这个答案是 3 年前写的,现在仍然正确。请阅读文档shopify.dev/themes/architecture/sections/section-schema#content,其中提到了The {% schema %} tag is a Liquid tag. However, it doesn’t output its contents, or render any Liquid included inside it.,所以不,你仍然不能。在提出 3 年前的答案之前,请仔细检查您的来源。
      • 伙计,您编写自己的架构,只需使用 if else 和拉变量,原理本身就有效。正如我所说,我的回答至少显示了如何提取每个产品结果/内容/元
      • @dExIT 我认为您混淆了您的术语。您显示的是 schema.org 它与 Shopify 部分架构无关。 {% schema %} 标签是 Shopify 部分文件所独有的,它控制自定义面板中显示的字段,但您目前显示的是描述页面内容的架构......虽然它们共享相同的名称 @987654325 @ 除了它们都是 JSON 对象之外,它们绝对没有任何共同点。
      猜你喜欢
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 2017-09-17
      • 2020-03-16
      相关资源
      最近更新 更多