Shopify新手技术求助:如何按分类展示对应博客文章?
Hey there! Let's get that duplicate article issue sorted for your Shopify multi-blog setup. Since you're using 5 separate blogs as your categories, the core problem right now is that your code is pulling all site-wide articles instead of targeting only the ones tied to each specific blog/category.
Here's how to rewrite your loops correctly:
1. If you're displaying all 5 blog categories on a single page (like a main blog hub)
Use the global blogs object to iterate through each of your blogs, then nest a loop for only that blog's articles inside:
<!-- Loop through each of your blogs (categories) --> {% for blog in blogs %} <section class="blog-category"> <!-- Display the blog/category title --> <h2>{{ blog.title }}</h2> <!-- Loop ONLY through articles belonging to this blog --> <div class="articles-grid"> {% for article in blog.articles %} <article class="article-card"> <h3><a href="{{ article.url }}">{{ article.title }}</a></h3> <p class="article-excerpt">{{ article.excerpt | strip_html | truncatewords: 25 }}</p> <a class="read-more" href="{{ article.url }}">Continue Reading</a> </article> {% empty %} <p>No articles in this category yet.</p> {% endfor %} </div> </section> {% endfor %}
2. If you're working on a single blog's template (e.g., blog.liquid)
If your individual blog pages are showing all articles instead of just their own, you probably used site.articles instead of the context-specific blog object. Replace your loop with this:
<!-- This pulls ONLY articles from the current blog/category --> {% for article in blog.articles %} <article class="single-article"> <h1>{{ article.title }}</h1> <div class="article-content"> {{ article.content }} </div> </article> {% endfor %}
3. If you need to target specific blogs (not all of them)
If you don't want to display every blog in your store, filter by your 5 category blog handles (you can find each blog's handle in your Shopify Admin under Blogs > [Blog Name] > URL handle):
<!-- List your 5 blog handles here --> {% assign target_blogs = ['fitness-tips', 'recipe-ideas', 'product-guides', 'customer-stories', 'industry-news'] %} {% for handle in target_blogs %} {% assign current_blog = blogs[handle] %} {% if current_blog %} <section class="blog-category"> <h2>{{ current_blog.title }}</h2> <div class="articles-grid"> {% for article in current_blog.articles %} <article class="article-card"> <h3><a href="{{ article.url }}">{{ article.title }}</a></h3> <p>{{ article.excerpt | strip_html | truncatewords: 25 }}</p> <a href="{{ article.url }}">Read More</a> </article> {% empty %} <p>No articles here yet—check back soon!</p> {% endfor %} </div> </section> {% endif %} {% endfor %}
Quick Checks to Ensure It Works:
- Double-check that each article is published to the correct blog in your Shopify Admin (when creating/editing an article, select the right blog from the "Blog" dropdown).
- Make sure you're not using
site.articlesanywhere in your loops—that's the main culprit behind duplicate articles across categories.
内容的提问来源于stack exchange,提问作者gnorbert

