输出Shopify文章ID

时间:2016-01-31 16:22:43

标签: shopify

Shopify博客文章的循环是:

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

我想知道是否可以按不同的ID输出文章。让我们说33,65,81。我有这个自定义字段,我可以写出我想要的ID,并输出:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果元字段包含单个文章ID并且作为整数保存在Shopfiy中,您基本上只需合并已有的两段代码:

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

    {% endif %}
{% endfor %}

如果您的元数据包含单个文章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 %}

如果您的元字段包含多个文章ID,以“33,65,81”等逗号分隔,则可以使用split filtercontains 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 %}

有关将元数据保存为字符串或整数的详细信息,请参阅:https://docs.shopify.com/api/metafield