【问题标题】:Hide text in Shopify product description在 Shopify 产品描述中隐藏文本
【发布时间】:2019-10-04 21:26:51
【问题描述】:

只是在 Shopify 中寻找一些指导。我想要做的是用 [#size] [/size] 标签(或类似标签)在产品描述中包装文本......然后抓取此文本并将其放入弹出窗口中。

我已经完成了大部分工作,以便它创建弹出按钮并且弹出窗口工作正常,甚至包括产品描述中的文本,但是,产品描述中的文本保留在那里而不是隐藏它进入弹出窗口,我现在正抓着稻草试图修复它......

任何想法都会受到高度评价,在此先感谢。

这是我在product-template.liquid中的代码

{% if product.description contains '[#size]' or product.description contains '[#video]' or product.description contains '[#other]' %}
      <div class="product-extra">
        {% if section.settings.show_product_size_chart and product.description contains '[#size]' %}
          {% assign shortcode_description = product.description | split: '[/size]' | first | split: '[#size]' | last %}
            <a class="product-size-chart fancybox" href="#product-size-chart"><i class="fas fa-ruler"></i> {{ 'products.product.sizing' | t }}
              <div id="product-size-chart" style="display: none;">
                {{ shortcode_description }}
              </div>
            </a>
        {% endif %}

        {% if section.settings.show_product_video and product.description contains '[#video]' %}
          {% assign shortcode_description = product.description | split: '[/video]' | first | split: '[#video]' | last %}
          <a class="product-video fancybox" href="#product-video"><i class="fas fa-play-circle"></i> {{ 'products.product.video' | t }}
            <div id="product-video" style="display: none;">
              {{ shortcode_description }}
            </div>
          </a>
        {% endif %}
        {% if section.settings.show_product_model and product.description contains '[#other]' %}
          <a class="product-model fancybox" href="#product-model"><i class="fas fa-info-circle"></i> {{ 'products.product.model' | t }}
            <div id="product-model" style="display: none;">
              {% assign shortcode_description = product.description | split: '[/other]' | first | split: '[#other]' | last %}
              {{ shortcode_description }}
            </div>
          </a>
        {% endif %}
      </div>
    {% endif %}

【问题讨论】:

    标签: shopify liquid shopify-template


    【解决方案1】:

    您可以创建一个名为 shortcode.liquid

    的 sn-p

    在 sn-p 中输入以下代码:

    {%- capture open_tag -%}[#{{tag}}]{%- endcapture -%}
    {%- capture close_tag -%}[/{{tag}}]{%- endcapture -%}
    
    {%- assign text_content = content | split: open_tag | last | split: close_tag | first -%}
    {%- capture remove_shortcode -%}{{open_tag}}{{text_content}}{{close_tag}}{%- endcapture -%}
    
    {%- assign new_content = content | remove: remove_shortcode -%}
    

    你这样调用sn-p:

    {%- include 'shortcode' content: product.content, tag: 'size' -%}
    

    您传递内容的位置(在本例中为 product.content)和您希望定位的短代码(在本例中为 size

    调用 sn-p 后,你有两个变量:

    {{text_content}}这将返回短代码之间的内容

    {{new_content}} - 这将返回新内容,但已删除短代码

    重要 - 此 sn-p 仅适用于同一简码的单个实例,也就是说,您不能有两个 size 简码实例。

    【讨论】:

    • 感谢您的回复,我真的很感激 :) 但是这似乎不起作用。 {{text_content}} {{new_content}} 都只返回整个产品描述。
    • @Jack 是的,我用与变量相同的名称调用了 sn-p。我更新了代码,所以它显示 tag 而不是短代码,它应该可以工作。
    • @Jack 对于简单的 sn-p,这变得更加复杂。最好看看这个:github.com/culturekings/shopify-shortcodes这是一个我一直在使用的工作短代码,但你需要自己编写提取逻辑。
    • 最后整理了,谢谢兄弟的帮助。对于任何感兴趣的人,我使用以下代码 {% if product.description contains '[#' %}
      {{ product.description |拆分:'[#' |第一个 }}
      {% else %}
      {{ product.description }}
      {% endif %}
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多