Shopify-如何从多个上传产品中调用特定的元场

发布于 2025-02-08 08:16:37 字数 1109 浏览 1 评论 0原文

我创建了一个“产品参考”和“产品列表” metafield。这使我可以将多个产品添加到我制作的metafield中。我要做的是显示我添加的产品的图像和标题,但是我想做的就是能够访问我添加的每种产品。

例如,我在metafield部分中添加了两个产品,但我只想在我的产品页面上显示其中一个。

我想单独访问我的产品的原因是我将使用Bootstrap,并且我想将每个产品图片分别添加到创建的Divs。

这是我试图调用我的metafield的代码的样子。 (它带来了这两张图片)

   {% if product.metafields.my_fields.custom_img.value != blank  %}

    <h3>Recommended Products</h3>
    
    {% assign recommended_products = product.metafields.my_fields.custom_img.value %}
    {% for product in recommended_products  %}     
    
      <a href="{{ product.url }}">
        {{ product.featured_image | image_url: width: 400 | image_tag }}
        
      </a>
   
    {% endfor %}

   {% endif %}

I created a "Product Reference" and "List of products" metafield. This allows me to add multiple products into the metafield that I made. What I'm trying to do is display images and titles of the products that I added but what I would like to do is able to access each of the products I added.
enter image description here

For example, I have added two products to my metafield section but I only want to display one of them on my product page.
enter image description here

The reason why I would like to access my products individually is I'm going to use bootstrap and I want to add each product picture individually to the divs I created.

Here's what my code looks like when I'm trying to call my metafield. (It brings both of the pictures)

   {% if product.metafields.my_fields.custom_img.value != blank  %}

    <h3>Recommended Products</h3>
    
    {% assign recommended_products = product.metafields.my_fields.custom_img.value %}
    {% for product in recommended_products  %}     
    
      <a href="{{ product.url }}">
        {{ product.featured_image | image_url: width: 400 | image_tag }}
        
      </a>
   
    {% endfor %}

   {% endif %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2025-02-15 08:16:37

如果您只想显示第一个最简单的方法是这样做

{% for product in recommended_products limit: 1  %}     
 <div> ... </div>

{% endfor %}

显示特定产品

{% for product in recommended_products  %}     

  {% if product.title == "title"  %}
    <div> ... </div>    
    {% break %}
  {% endif %}
{% endfor %}

,或者如果您想对Cylce Liquid拥有更多控制权,则要 =“ https://shopify.dev/api/liquid/objects/for-loops” rel =“ nofollow noreferrer”> https://shopify.dev/api/liquid/liquid/objects/for-lo-lo-lo-lo-lo-lor-loops

例如您也可以这样做

{% for product in recommended_products  %}     
 <div>... </div>
 {% if forloop.index0 > 1 %}
    {% break %}
  {% endif %}
{% endfor %}

以显示前两个产品。

If you want to display only the first the easiest way is to do

{% for product in recommended_products limit: 1  %}     
 <div> ... </div>

{% endfor %}

Or if you want to display a particular product

{% for product in recommended_products  %}     

  {% if product.title == "title"  %}
    <div> ... </div>    
    {% break %}
  {% endif %}
{% endfor %}

If you want to have more control over the for cylce liquid offers you the forloop object https://shopify.dev/api/liquid/objects/for-loops

For example you could also do

{% for product in recommended_products  %}     
 <div>... </div>
 {% if forloop.index0 > 1 %}
    {% break %}
  {% endif %}
{% endfor %}

to display the first 2 products.

三寸金莲 2025-02-15 08:16:37

您可以像液体中的任何列表一样访问您创建的Metafield,例如,如果您想访问列表中的第一个产品

{% assign first_product = product.metafields.my_fields.custom_img.value | first %}

,或者可以访问列表中的任何特定产品:

{% comment % }Access second product {% endcomment %}
{% liquid
  assign second_product = product.metafields.my_fields.custom_img.value[1]
  if second_product != blank
    echo '<h1>second_product.title</h1>'
  endif
%}

You can access the metafield that you've created just like any list in liquid, for example, if you want to access the first product in the list you can do

{% assign first_product = product.metafields.my_fields.custom_img.value | first %}

Or you can access any specific product in the list like so:

{% comment % }Access second product {% endcomment %}
{% liquid
  assign second_product = product.metafields.my_fields.custom_img.value[1]
  if second_product != blank
    echo '<h1>second_product.title</h1>'
  endif
%}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文