Re: How can I get a list of all blogs in liquid?

How can I get a list of all blogs in liquid?

Leoster
Tourist
10 0 1

I'm trying to list all blogs on my site with liquid, something like:

 

{% for blog in blogs  %}
  <h2>{{ blog.title }}</h2>
{% endfor %}


But this doesn't seem to work. Is there any way to do this?

Replies 5 (5)

Serj94
Shopify Partner
30 3 4

Code looks fine.

Do you have any blog in your shop? Is that code inside a visible section?

 

Blogs in liquid are all shop's blog topics.

Each blog topic has it's own name, articles etc. See liquid blogs object https://shopify.dev/docs/api/liquid/objects/blogs

If the answer is useful, give it a like or mark as the accepted solution
Leoster
Tourist
10 0 1

Doesn't work… the blogs object only allows you to retrieve a blog by specific handle. It won't allow me to loop through them. 

pawankumar
Shopify Partner
553 74 95

Hi @Leoster 
I don't think, there is a blogs object in shopify that you can use
But you can use it by passing a blog handle like if you want to show blog articles under a blog, the you can use a code like this

{% for article in blogs["news"].articles  %}
  <h2>{{ article.title }}</h2>
{% endfor %}

Here "news" is blog handle,  when you go to Online store --> Blog Posts --> Manage Blogs
and then view a blog on frontend then last part of the url (after blogs/) like yourstore/blogs/news is blogs handle

I think, it will help you to get an idea

Thanks!

- Need a Shopify developer? Chat on WhatsApp +91-9467121281
- Coffee Tip: Buymeacoffee  | Email: thepkpawankumar@gmail.com
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
Best regards,
Pawan
Leoster
Tourist
10 0 1

This doesn't solve the problem, which is to show all blogs programatically. Without being able to iterate all blogs, it means every time I add a blog I would have to go update templates manually. 

I assume Shopify intended this to be something you'd set once while setting up a shop and rarely if ever touch again… but I am using blogs to separate out content categories, which are currently around 25 and will scale to at least 100. 

pawankumar
Shopify Partner
553 74 95

Hi @Leoster 
I don't think, there is some other way for this till now
Either you can use a static blogs list like this:

 {% assign blog_handles = "blog1,blog2,blog3" | split: "," %}

   {% for handle in blog_handles %}

        {% for article in blogs[handle].articles %}

               <h2><a href="{{ article.url }}">{{ article.title }}</a></h2>
         {% endfor %}

  {% endfor %}

Or Use a linked list and iterate like this

{% 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 %}

The other way is you can use shopify api and create an app for this
Rest API Reference: https://shopify.dev/docs/api/admin-rest/2024-04/resources/blog#get-blogs

Thanks!

- Need a Shopify developer? Chat on WhatsApp +91-9467121281
- Coffee Tip: Buymeacoffee  | Email: thepkpawankumar@gmail.com
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
Best regards,
Pawan