Loop through all articles in all blogs

Highlighted
Shopify Partner
10 0 3

I would like to display article elements (title, image) on a page based on how the article is tagged.

 

As there is no object for all articles, it's my understanding that I would need to loop through all blogs, and then all articles in those blogs, and then read the article tags and handle accordingly.

 

There was a confirmed solution in this post:

 

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

 

which is not working for me. Though that post was from March 2019, so I don't know if there was some undocumented change (I have not found anything in the Shopify changelog related to this).

 

I tried to simplify things even further - just displaying the blog.handle in my template:

{% for blog in blogs %}
  {{blog.handle}}
{% endfor %}

And even this isn't working.

 

Documentation here is sort of confusing - as it doesn't show looping through all blogs, just accessing the global blog object for a specific blog. This makes me think that maybe looping through all blogs isn't possible, and you have to explicitly define the blog before looping through articles?

 

Does anyone have any ideas here?

0 Likes
Highlighted
Shopify Partner
242 59 103

Hi @Ryan_Kelly,

 

I understand that you want to access the Blog object from your custom page. The reason you're having trouble is that the Blog object is not accessible from page templates.

 

The workaround for this is to create a special linked-list for your blogs – and then target that linked-list to pull the Blog objects into your code:

 

Step 1: Create the special "Blogs" linked-list:

  1. From your Shopify admin, go to Online Store > Navigation.
  2. Click the Add menu button.
  3. Enter Blogs as the Title.
  4. Repeat the following for each blog category that you want listed:
    1. Click Add menu item.
    2. Click in the Link box and select Blogs from the pop-up menu.
    3. Select the Blog that you want displayed.
  5. Click Save menu.

 

Step 2: Edit your custom page template

  1. Add the following code to your custom page template:
    {% for link in linklists.blogs.links %}
      {% assign blog = link.object %}
      {% for article in blog.articles %}
        <h2><a href="{{ article.url }}">{{ article.title }}</a></h2>
      {% endfor %} 
    {% endfor %}
  2. Click Save.

 

Let me know if this solves your problem or if you need further help!

Brian | Shopify Partner | Ecommerce Consultant
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution  
 - Need further assistance? Visit www.BrianAtWork.com

2 Likes
Highlighted
Shopify Partner
10 0 3

Hi @BrianAtWork -

 

I actually got around this by explicitly defining blog handles (as on my store I only have two blogs) to loop through in my page template, and creating a list of article handles like so:

  {% assign blog_handles = "culture,features" | split: "," %}
  {% for handle in blog_handles %}
    {% for article in blogs[handle].articles %}
      {% if article.tags contains blog_tag %}
        {% assign article_handles = article_handles | append: article.handle | append: "," %}
      {%endif%}
    {% endfor %}
  {% endfor %}

Then I loop through the defined article handles to access them.

2 Likes
Highlighted
New Member
2 0 2

Hi there,

Is it possible to then filter this by tags? We have the same tag "Learn" across several different blogs and I would like to pull only blog articles tagged with "Learn" onto this page.
Any  help would be much appreciated

0 Likes
Highlighted
Shopify Partner
10 0 3

Yes, that's what I'm doing above (though I didn't define the variable blog_tag) with this block:

 

      {% if article.tags contains blog_tag %}
        {% assign article_handles = article_handles | append: article.handle | append: "," %}
      {%endif%}

But note that you need to explicitly define the blog handles as Shopify isn't able to loop through all blog posts (needs to loop through blogs, and then posts within that blog).

 

Another thing I discovered is that, with enough posts, you can hit pagination limit. So a more complete block would be:

 

{% assign blog_handles = "blog-handle-1,blog-handle-2,blog-handle-etc" | split: "," %}
{% assign blog_tag = "your-blog-tag" %}
  
{% for handle in blog_handles %}
  {% paginate blogs[handle].articles by 100 %}
      {% for article in blogs[handle].articles %}
        {% if article.tags contains blog_tag %}
          {% assign article_handles = article_handles | append: article.handle | append: "," %}
        {%endif%}
      {% endfor %}
   {% endpaginate %}
{% endfor %}

Which will give you the article handles you need, and you can access/display those with something like:

 

{% for handle in article_handles limit: home_page_articles %}
   {% assign article = articles[handle] %}
   <h2>{{ article.title }}</h2>
{% endfor %}

(Yeah, it's weird you can't loop through all articles, but you can refer to the complete articles object)

 

I'm setting the blog_tag variable through the theme customizer or settings_data.json file.

0 Likes