2014-11-14 44 views
0

我想利用Shopify linklist界面在主页上显示内容。很遗憾,您无法直接将博客文章添加到链接列表 - 只能通过它的网址。所以我不能使用液体来检索显示文章内容所需的信息。从Shopify API向博客ID和文章ID添加文章数据

另一种方法是用户可以指定一个普通的网址。因此,让他们想通过输入这样一个网址来包含一篇文章:/ blogs/news/18059703-article-title

我想解析这个url,并使用Shopify jquery api来填写所需的信息。

我知道我可以使用液体处理链接URL来获取博客ID和文章ID。然后,我可以将它们注入作为链表的输出属性.....

{% for link in linklists.homepage.links %} 
    {% if link.url contains 'blogs' %} 
    {% capture partial_url %}{{ link.url | remove: '/blogs/' }}{% endcapture %} 
    {% capture blog_title %}{{ partial_url | split: "/" | first }}{% endcapture %} 
    {% capture blog_id %}{{ blogs[blog_title].id }}{% endcapture %} 
    {% capture article_handle %}{{ partial_url | split: "/" | last }}{% endcapture %} 
    {% capture article_id %}{{ article_handle | split: "-" | first }}{% endcapture %} 

    <div class="panel" data-blog-id="{{ blog_id }}" data-article-id="{{ article_id }}"> 
     <a href="{{ link.url }}"> 
     <div class="image"> 
     <img src="placeholderimage.jpg" /> 
     </div> 
     <div class="text-wrap"> 
     <div class="text"> 
     <h2>ARTICLE TITLE</h2> 
     <h3>ARTICLE AUTHOR</h3> 
     <h4>ARTICLE PUBLISHED DATE</h4> 
     <p>First 50 characters or so of the article</p> 
     </div> 
     </div> 
     </a> 
    </div> 


    {% endif %} 
{% endfor %} 

既然我已经包括了jQuery API脚本

{{ 'api.jquery.js' | shopify_asset_url | script_tag }} 

什么将是jQuery的/代码访问并输出上述代码中所需的文章信息?

最终,如何将指定文章的值作为变量访问,以便在jquery中使用以插入到dom中。

如果您可以提供如何提取出文章中用作图像的第一张图像SRC,则可获得奖励积分。

回答

1

既然你已经有了博客标题,我想尝试这样的事:

{% for link in linklists.homepage.links %} 
    ... 
    {% for article in blogs[blog_title].articles %} 
    {% if link.url == article.url %} 
     <h2>{{ article.title }}</h2> 
     <h3>{{ article.author }}</h3> 
     <h4>{{ article.published_at }}</h4> 
     ... 
    {% endif %} 
    {% endfor %} 
    ... 
{% endfor %} 
+0

良好的通话!我唯一的问题就是如果有任何限制,我知道有一个系列的产品。不幸的是,我没有任何博客文章超过50篇的客户的经验。但是,我想知道博客中是否有500篇文章,如果代码将查看所有文章。 – 2014-11-19 18:16:08

+0

据我所知,该限制只适用于收藏品中的产品......虽然我刚刚检查了[Article API文档](http://docs.shopify.com/api/article#index)并找到了这个:'limit:结果量(默认:50)(最大值:250)'。不知道这个限制是否也适用于'blog.articles'的液体,但可能是一个好主意,只为确保自己而测试它。 – 2014-11-20 11:51:52