Loop through all articles in all blogs

Highlighted
Shopify Partner
9 0 1

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
Shopify Partner
236 59 70

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

0 Likes
Shopify Partner
9 0 1

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.

0 Likes