简体   繁体   English

通过文章ID输出Shopify文章

[英]Output Shopify Article by Article ID

The loop for Shopify Blog Articles is: Shopify博客文章的循环是:

{% for article in blog.articles %}
{% endfor %}

I'm wondering if it's possible to output articles by different ID. 我想知道是否可以通过不同的ID输出文章。 Let's say 33, 65, 81. I have this custom field where I can write the ID's I want and is outputted by: 假设是33、65、81。我有一个自定义字段,可以在其中写入所需的ID,并通过以下方式输出:

{% if article.metafields.c_f.recommended_post %}{% else %}
{% endif %}

Any ideas? 有任何想法吗?

If the metafield contains a single article ID and is saved in Shopfiy as an integer, you can basically just combine the two pieces of code you already have: 如果该元字段包含一个商品ID,并且在Shopfiy中保存为整数,则基本上可以将已经拥有的两段代码结合起来:

{% for article in blog.articles %}
    {% if article.id == article.metafields.c_f.recommended_post %}
        {% comment %} Found recommended article {% endcomment %}
    {% else %}

    {% endif %}
{% endfor %}

If your metafield contains a single article ID but is saved in Shopify as a string you need one extra step to covert the metafield value to an integer: 如果您的元字段只包含一个商品ID,但又以字符串形式保存在Shopify中,则需要采取额外的步骤将元字段的值转换为整数:

{% for article in blog.articles %}
    {% assign recommended_post = article.metafields.c_f.recommended_post | plus: 0 %}
    {% if article.id == recommended_post %}
        {% comment %} Found recommended article {% endcomment %}
    {% else %}

    {% endif %}
{% endfor %}

If your metafield contains multiple article IDs, separated by commas like "33, 65, 81" you can use the split filter and contains operator , and convert your article id to a string instead: 如果您的元字段包含多个文章ID,并用逗号分隔,例如“ 33、65、81”,则可以使用拆分过滤器包含operator ,然后将您的文章ID转换为字符串:

{% assign recommended_ids = article.metafields.c_f.recommended_post | split: ', ' %}
{% for article in blog.articles %}
    {% assign article_id = article.id | append: '' %}
    {% if recommended_ids contains article_id %}
        {% comment %} Found one of many recommended articles {% endcomment %}
    {% else %}

    {% endif %}
{% endfor %}

For more information on saving metafields as either strings or integers see: https://docs.shopify.com/api/metafield 有关将元字段保存为字符串或整数的更多信息,请参见: https ://docs.shopify.com/api/metafield

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM