温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Getting Blog JSON from Shopify on storefront and possibly filter by tag
shopify

其他 - 从店面的Shopify获取Blog JSON,并可能按标记过滤

发布于 2020-03-27 15:39:40

我需要从店面获取JSON格式的博客文章,因为您可以在任何单个页面请求上获取的文章数量是50。一个额外的好处是能够基于标签过滤文章。

可以通过AJAX API从Shopify店面获取JSON,但仅限于productscart我知道您可以为其他所有内容创建备用模板,但是如何为博客做呢?

我已经尝试过了,但是不起作用:

https://domain.myshopify.com/admin/blogs/blog_id/articles.json

查看更多

查看更多

提问者
Modermo
被浏览
22
drip 2020-01-31 16:49

您谈论的是Storefront,但您提供的是Admin API URL。如果不使用GraphQL或Rest API,就无法从店面请求管理员!

Liquid way

You are limited by 50 articles if you don't overwrite the paginate on the storefront.

But if you overwrite it you can get as much as you like. ( have in mind that the larger the article pool the longer the DOM will load )

Example:

{% paginate blog.articles by 9999 %}
  {% for article in blog.articles %}

  {% endfor %}
{% endpaginate %}

You can create a separate blog template and request it with AJAX and add the tag to the end as well.

So if you create a blog template called blog.ajax.liquid your request will be something like so: /blogs/news/tagged/featured?view=ajax and it will return the html for the new template filtered by the tag featured.


GraphQL way

The other way is to use the storefront GraphQL in order to get the articles.

You will need to create a private app and allow Read content like articles, blogs, and comments in order to use this.

Example query:

{
  blogByHandle(handle:"news"){
    articles(first: 50, query:"tag:featured") {
      edges {
        node {
          title
        }
      }
    }
  }
}

当然,这将返回50个带有名为标记的文章标题featured,您可以添加更多您希望查询返回的字段。

REST API

另一种方法是使用REST API。

您仍然需要创建一个私有应用程序,但是您必须仅允许“博客和文章”阅读权限,而没有写权限。此外,应禁用所有其他权利,以免其他人修改您的商店数据。

AJAX网址将如下所示: https://API_KEY:API_PASSWORD@YOUR_STORE.myshopify.com/admin/api/2020-01/blogs/BLOG_ID/articles.json?tag=featured

我不推荐这种方法,但是仍然可以。


在这里,您可以选择自己喜欢的方式。