2014-11-14 55 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