Solved

Displaying Metafields in front end

IOANNARITSONI
Shopify Partner
3 0 1

I want to have recipe blogs on my site and I want each recipe to have the products that I used from my shop.

 

i made a meta field in blog posts of type product list

Screenshot 2024-05-28 at 4.51.22 PM.png

Im trying to add it in the template of my blog posts with liquid but the product list doesn't seem to get rendered 

 

I added this in main-article.liquid

   {% if article.metafields.custom.ingredients %}
  <div class="ingredients">
    <h3>Ingredients</h3>
    <ul>
      {% for product_id in article.metafields.custom.ingredients %}
        {% assign product_id_cleaned = product_id | remove: "gid://shopify/Product/" %}
        {% assign product = all_products[product_id_cleaned] %}
        {% if product %}
          <li>
            <a href="{{ product.url }}">{{ product.title }}</a>
          </li>
        {% else %}
          <li>Product not found</li>
        {% endif %}
      {% endfor %}
    </ul>
  </div>
{% else %}
  <p>No ingredients metafield found for this article.</p>
{% endif %}

All it shows in the front end is "Ingredients" and an empty list 

Screenshot 2024-05-28 at 5.00.14 PM.png

Where am I going wrong?

Accepted Solution (1)

Guleria
Shopify Partner
3096 608 866

This is an accepted solution.

Try this

   {% if article.metafields.custom.ingredients %}
  <div class="ingredients">
    <h3>Ingredients</h3>
    <ul>
{% assign list_metafield = article.metafields.custom.ingredients.value %}
      {% for item in list_metafield  %}
         
          <li>
            <a href="{{ item.url }}">{{ item.title }}</a>
          </li> 
      {% endfor %}
    </ul>
  </div>
{% else %}
  <p>No ingredients metafield found for this article.</p>
{% endif %}
- If helpful then please Like and Accept Solution.
- Want to modify or custom changes or bug fix on store Hire me.
- Email: guleriathakur43@gmail.com - Skype: navrocks1 ,
- Try GEMPAGES a great page builder
-Advance Search Filter

View solution in original post

Replies 3 (3)

Guleria
Shopify Partner
3096 608 866

This is an accepted solution.

Try this

   {% if article.metafields.custom.ingredients %}
  <div class="ingredients">
    <h3>Ingredients</h3>
    <ul>
{% assign list_metafield = article.metafields.custom.ingredients.value %}
      {% for item in list_metafield  %}
         
          <li>
            <a href="{{ item.url }}">{{ item.title }}</a>
          </li> 
      {% endfor %}
    </ul>
  </div>
{% else %}
  <p>No ingredients metafield found for this article.</p>
{% endif %}
- If helpful then please Like and Accept Solution.
- Want to modify or custom changes or bug fix on store Hire me.
- Email: guleriathakur43@gmail.com - Skype: navrocks1 ,
- Try GEMPAGES a great page builder
-Advance Search Filter
IOANNARITSONI
Shopify Partner
3 0 1

this worked thank you!

PageFly-Amelia
Shopify Partner
452 144 206

Hello @IOANNARITSONI 

This is Amelia at PageFly - Shopify Advanced Page Builder app.

 

You can try 

 {% if article.metafields.custom.ingredients %}
  <div class="ingredients">
    <h3>Ingredients</h3>
    <ul>
{% assign list_metafield = article.metafields.custom.ingredients.value %}
      {% for item in list_metafield  %}
          <li>
            <a href="{{ item.url }}">{{ item.title }}</a>
          </li> 
      {% endfor %}
    </ul>
  </div>
{% else %}
  <p>No ingredients metafield found for this article.</p>
{% endif %}

Hoping my solution helps you solve your problem.

Best regards,

Amelia | PageFly

Please let me know if it works by giving it a Like or marking it as a solution!


PageFly - #1 Page Builder for Shopify merchants.


All features are available from Free plan. Live Chat Support is available 24/7.


Optimize your store’s mobile shopping experience with the Blum theme.