ok, I have the links pulled out, what next? is there any way to link them to blogs?
Topic summary
A developer is working on displaying blog posts on product cards in Shopify and wants to implement a fallback system: if product-specific metafields contain blog post links, use those; otherwise, display globally assigned blog posts.
Current Setup:
- Uses a single template that globally displays blog posts for all products
- Has existing code that loops through blogs and articles to find and render two posts
- Already created metafields (
related_product_with_blog_post) storing two blog post links per product
Solution Provided:
A community member suggested simplifying the complex existing code by:
- Storing comma-separated article links in metafields
- Parsing these in Liquid (rather than JavaScript)
- Iterating over the metafield links using a
forloop - Extracting blog and article handles by splitting the URLs
- Rendering each article using the
blog-cardsnippet with proper parameters
Code Approach:
The solution involves splitting metafield URLs by /blogs/ to extract handles, then using Liquid objects to pull specific articles and render them with {% render "blog-card" %}. This method avoids performance issues from overly complex logic and provides cleaner conditional rendering based on metafield presence.