I have quite a few articles under different blogs, and on my website blog page I would like to display ALL my blog articles with different blog categories. How can I achieve that? I can do customize coding.
https://tmgindustrial.ca/blogs/news
By default, the theme only allows me fetch blog articles under one blog category.
Help is appreciated! Thanks in advance.
Hi @tmg1101 .
One solution would be do one main Blog and use Tags in each Blog Article:
- Create your blog articles as normal - Tag each article with your desired category.
- You can add multiple tags per article.
- Add Category Navigation:
- Go to Online Store > Themes > Edit Code - Open Sections folder - Find main-blog.liquid (Dawn Theme)
- Add a similar code to create a category menu. In your case, perhaps before
.
{%- if blog.all_tags.size > 0 -%}
- All
{%- for tag in blog.all_tags -%}
- {{ tag }}
{%- endfor -%}
{%- endif -%}
Hi there, thanks for replying!
The reason we are having multiple blogs is because there are a few landing pages for our product lines. And on each of the landing pages we only want to display the blog articles that are related to the product line.
So in this case I can’t just put all blogs under one blog. But is there anyway to fetch blog articles by specific tags?
@tmg1101
Yes, it’s totally possible to handle this use case! You can create a custom section that allows you to:
- Select which blogs you want to pull from (up to however many blogs you want… I did two for simplicity)
- Filter articles by specific tags
- Control how many articles appear
To implement this:
- Create a new section file (e.g., ‘section-blog-join.liquid’)
- Add it to your page template
- In the theme editor, you can then select the blogs and tag for each landing page
section-blog-join.liquid would look like this.
style.css (basic styling)
html + liquid
{% assign post_limit = section.settings.posts_limit | default: 3 %}
{% assign all_articles = '' | split: '' %}
{% if section.settings.blog_1 != blank %}
{% assign all_articles = all_articles | concat: blogs[section.settings.blog_1].articles %}
{% endif %}
{% if section.settings.blog_2 != blank and section.settings.blog_2 != section.settings.blog_1 %}
{% assign all_articles = all_articles | concat: blogs[section.settings.blog_2].articles %}
{% endif %}
{% assign shown_articles = 0 %}
{% for article in all_articles %}
{% if section.settings.tags == blank or article.tags contains section.settings.tags %}
{% if shown_articles < post_limit %}
{% assign shown_articles = shown_articles | plus: 1 %}
{% if article.image %}
{{ article.image | image_url: width: 400 | image_tag }}
{% endif %}
### {{ article.title }}
{% if article.excerpt != blank %}
{{ article.excerpt | strip_html | truncatewords: 30 }}
{% endif %}
Read more
{% endif %}
{% endif %}
{% endfor %}
{% if section.settings.tags != blank and shown_articles == 0 %}
No articles found with the tag "{{ section.settings.tags }}"
{% endif %}
schema
{% schema %}
{
"name": "Blog Posts Join",
"settings": [
{
"type": "blog",
"id": "blog_1",
"label": "Select First Blog"
},
{
"type": "blog",
"id": "blog_2",
"label": "Select Second Blog (Optional)"
},
{
"type": "text",
"id": "tags",
"label": "Filter by tag",
"info": "Enter the exact tag name (case sensitive)."
},
{
"type": "range",
"id": "posts_limit",
"min": 1,
"max": 3,
"step": 1,
"label": "Number of posts",
"default": 3
}
],
"presets": [
{
"name": "Blog Posts Join",
"category": "Blog"
}
]
}
{% endschema %}