Displaying Metafields in front end

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
4103 803 1155

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 %}
- Custom themes, UI/UX design, ongoing maintenance & support.
- Drop an email   if you are looking for quick fix or any customization
- Email: guleriathakur43@gmail.com Skype: live:navrocks1
- Try GEMPAGES a great page builder

View solution in original post

Replies 3 (3)

Guleria
Shopify Partner
4103 803 1155

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 %}
- Custom themes, UI/UX design, ongoing maintenance & support.
- Drop an email   if you are looking for quick fix or any customization
- Email: guleriathakur43@gmail.com Skype: live:navrocks1
- Try GEMPAGES a great page builder
IOANNARITSONI
Shopify Partner
3 0 1

this worked thank you!

PageFly-Amelia
Shopify Partner
626 165 238

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!


➜ Optimize your Shopify store with PageFly Page Builder (Free plan available) 
➜ Weekly updated Shopify tutorials on YouTube 


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