Dynamic Collection Links: How to Display Shopify Metafields on Product Pages
Hey there, fellow store owners! As a Shopify expert who spends a lot of time digging through our fantastic community forums, I often come across those "aha!" moments that are just too good not to share. Today, I want to talk about something super practical that can really enhance your customers' shopping experience: creating dynamic links from your product pages back to the specific collections that feature those products.
It sounds simple, right? "Just link back to the collection." But when you've got products appearing in multiple collections, or you want a flexible, future-proof way to manage these links without hardcoding, that's where things get interesting. This is exactly what our community member Kairike tackled head-on, and their journey and solution are a perfect example of community problem-solving.
The Challenge: Linking Products to Collections Dynamically
Kairike's goal was clear: they had set up a collection (reference type) metafield for their products and wanted to display that metafield as a clickable link on the product page. The idea was to allow visitors to easily navigate back to the relevant collection. A brilliant idea for user experience!
However, as Kairike quickly found out, simply adding a reference type metafield to a theme block in Shopify's Horizon theme (or any theme, for that matter) doesn't automatically render it as a clickable link. It often just shows the collection title, or nothing at all, without the necessary HTML structure to make it a link.
Kairike tried a few approaches with the Custom Liquid block, which is usually our go-to for these kinds of custom display needs. Let's look at what they tried:
Here was Kairike's first attempt, which managed to display the collection title but didn't make it a link:
{% assign col = [product.metafields.custom.kollektsioon.value](http://product.metafields.custom.kollektsioon.value) %}
{% if product.metafields.custom.kollektsioon.value != blank %} {% assign col = product.metafields.custom.kollektsioon.value %} ← Back to {{ col.title }} {% endif %}
And then a second attempt, which unfortunately failed to display anything at all:
{% assign target_collection = collection.metafields.custom.kollektsioon.value %} {% if target_collection != blank %} {{ target_collection.title }} {% endif %}
Notice in the second snippet how collection.metafields.custom.kollektsioon.value was used? When you're on a product page, the primary object is product, not collection. So, trying to access collection.metafields in that context wouldn't yield the desired result, as the collection object might not be defined or be the one you're expecting.
The "Aha!" Moment: Kairike's Self-Found Solution
After some persistence, Kairike cracked the code themselves and generously shared the working solution back with the community. And that's the beauty of these forums!
Here's the correct and working code snippet that Kairike found:
{% assign target_collection = product.metafields.custom.kollektsioon.value %} {% if target_collection != blank %} {{ target_collection.title }} {% endif %}
This snippet is elegant and effective. Let's break down why it works so well:
{% assign target_collection = product.metafields.custom.kollektsioon.value %}: This line correctly accesses the metafield. When you create a "collection reference" metafield for products, its.valueproperty actually returns the entire collection object. This is key!{% if target_collection != blank %}: A crucial check! This ensures that the code only tries to render the link if a collection has actually been assigned to the product via the metafield. It prevents errors and keeps your site clean.{{ target_collection.title }}: This is where the magic happens. Becausetarget_collectionis a full collection object, we can directly access its.urlproperty for the link's destination and its.titleproperty for both the link text and the title attribute.
Step-by-Step: Implementing Dynamic Collection Links
Ready to add this functionality to your own store? Here's how you can implement Kairike's solution:
Step 1: Create Your Collection Reference Metafield
- From your Shopify admin, go to Settings > Custom data.
- Under the Metafields section, click on Products.
- Click Add metafield.
- Give your metafield a descriptive Name (e.g., "Related Collection" or "Back to Collection"). The Namespace and key will be generated automatically (e.g.,
custom.related_collection). Make a note of this, as you'll need it for the code. - Select Content type. Choose Collection under the "Reference" section.
- Click Save.
Step 2: Assign a Collection to Your Product
- Go to Products in your Shopify admin and select the product you want to edit.
- Scroll down to the Metafields section.
- You'll see your newly created metafield (e.g., "Related Collection"). Click on it and select the specific collection you want this product to link back to.
- Click Save on the product page.
Step 3: Add the Custom Liquid to Your Product Template
- From your Shopify admin, go to Online Store > Themes.
- Find your current theme and click Customize.
- Navigate to a Product template (e.g., Default product).
- In the theme editor, click Add section (or Add block, depending on where you want it).
- Search for and select Custom Liquid.
- Paste Kairike's working code into the Custom Liquid block:
{% assign target_collection = product.metafields.custom.kollektsioon.value %} {% if target_collection != blank %} ← Back to {{ target_collection.title }} {% endif %} - Important: Make sure to replace
product.metafields.custom.kollektsioon.valuewith the actual namespace and key of your metafield (e.g.,product.metafields.custom.related_collection.value). - (Optional) You can add a little arrow or text like "← Back to" before
{{ target_collection.title }}for better context, as I've included in the code above. - Click Save.
And that's it! You'll now have a dynamic, clickable link on your product page that takes customers directly back to the collection you've assigned via the metafield. This is a fantastic way to improve site navigation and keep customers engaged with your curated collections.
A big thank you to Kairike for sharing their process and solution with the community. It's these kinds of insights that truly make the Shopify ecosystem thrive! If you've been looking for a flexible way to manage these kinds of links, this metafield approach is definitely one to add to your toolkit. Happy customizing! ![]()