Adding a collection metafield as a link to product page

Hi!

I have created a collection (reference type) metafield and added collection to a product. I’m trying to display that metafield as a link on product page for visitors to be able to return to collection. However, the reference type metafield cannot be directly added to any theme block in the Shopify’s Horizon theme.

I have tried the Custom Liquid block with various versions of code but cannot get it to work. Perhaps anybody here can figure it out. Maybe I’m close but missing something.

This first code snippet displays it but does not 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 %}

This code snippet currently fails to display anything:

{% assign target_collection = collection.metafields.custom.kollektsioon.value %} {% if target_collection != blank %} {{ target_collection.title }} {% endif %}

Thank you in advance!
K. :folded_hands:

I was able to solve it myself, the correct code is:

{% assign target_collection = product.metafields.custom.kollektsioon.value %} {% if target_collection != blank %} <a href="{{ target_collection.url }}" class="link" title="{{ target_collection.title }}"> {{ target_collection.title }} </a> {% endif %}