【问题标题】:shopify - Is there a way to create cms-block like "sections" to access from various sites (product/collection/page)shopify - 有没有办法创建类似于“部分”的 cms 块以从各种站点(产品/集合/页面)访问
【发布时间】:2023-12-07 18:38:01
【问题描述】:

我实际上正在开发一个 shopify 主题,并且我正在寻找一种方法来在单个位置创建部分或部分块之类的内容,可以在任何模板液体中调用它以在各种情况下重用创建的块在整个主题中。

诸如信息块、横幅或整个部分之类的东西,可以显示在集合中,也可以显示在某些产品中,具体取决于产品价值。

因此可以在所见即所得或部分(块)编辑器中轻松更改内容,并在整个主题中进行更改。

我通过创建一个单独的博客来管理类似的事情,我用它来创建可以在任何主题文件中调用的全局可访问内容。

我不满意,因为文章必须发布才能看到,因此当您知道博客网址时可以访问。

shopify 中是否有类似“cms-block”的功能或具有这些功能的应用?

有没有比以下更常见或更好的方法:

   {% if condition==true %}
      <div class="blog-insert-class">
        {% assign article = articles['BlogName/ArticleName'] %}
              {{ article.content }}
      </div>
   {% endif %}

【问题讨论】:

  • 也许值得看看这个 Shopify 替代 Wordpress 短代码(不确定您是否熟悉它们)。听起来它可能适合您的需求 - github.com/culturekings/shopify-shortcodes
  • 这实际上是一个非常有趣的 git-repo,但不是我正在寻找的,因为我没有完全了解数据的来源。我宁愿想到我从 Magento 知道的 CMS-Blocks 之类的东西,我可以在任何情况下调用。

标签: content-management-system shopify liquid shopify-app shopify-template


【解决方案1】:

您必须创建自定义钩子并按照@McNab 提到的类似方式使用它们,但不要输入全部内容。

例如,如果我们以您的示例为例,我们可以创建一个名为 [article] 的短代码。我们将为其添加句柄属性,因此它将变为[article handle="some-handle"]

您需要在内容中的某处输入上述简码。然后你可以使用@McNab 提到的提供的短代码,或者你可以编写一个自定义的。

对于自定义的,您需要创建一个 sn-p:

article-shortcode.liquid 使用以下代码:

<div class="blog-insert-class">
  {% assign article = articles[article-shortcode] %}
        {{ article.content }}
</div>

之后,您需要获取内容并对其进行修改以检查那里是否存在短代码。

所以是这样的:

{%- assign content = page.content -%}
{%- assign content_arr = content | split: '[article handle="' -%}

{%- if page.content contains '[article handle="' -%}
  {% comment %}Get the handle{% endcomment %}
  {%- assign article_handle = content_arr | last | split: '"]' | first -%}

  {% comment %}get the content after the shortcode{% endcomment %}
  {%- assign right_content = content_arr | last | split: '"]' | last -%} 

  {% comment %}save the content without the shortcode{% endcomment %}
  {%- assign content = content_arr | first | append: right_content -%} 
{%- endif -%}

{{ content }}

{% comment %}Call this where ever you like on the page{% endcomment %}
{%- if article_handle.size > 0 -%}
  {%- include 'article-shortcode' with article_handle -%}
{%- endif -%}

这是@McNab 提到的简码的更基本和精简版本。

但这是显示动态部分并进行某种查询的唯一方法之一(除了元字段)。

【讨论】: