Hiding metafields in blog posts when empty

Hi all, I’ve spent hours trying to hide metafields in blog posts when they are empty and I can’t get anything to work.

I want to add metafields for a featured product and a featured collection at the end of my blog posts: but I want to hide them when they’re not assigned. I’ve set up the metafields (blog-product and blog-collection).

I’ve watched/read several solutions and haven’t gotten any to work. The following one responded, but hides ALL metafields - whether they’re assigned or not:

{% if collection.metafields.custom.blog-product == blank %}

#shopify-section-template--16710703251698__featured_product_3JWUiz {display: none; }

{% endif %}

As you can see, I added them within the shop editor on the “blog post” settings.

Does anyone have any solutions for this? It’s a handy feature, if I can get it to display only when it’s populated :slightly_smiling_face:

@kristen001

i suggest you to do not display none make a condition like below that if the field is empty there will be nothing to show.

{% if collection.metafields.custom.blog-product == blank %}

{% else %}

{{ collection.metafields.custom.blog-product }}

{% endif %}

OR 2nd Condition will be

{% if collection.metafields.custom.blog-product != blank %}

{{ collection.metafields.custom.blog-product }}

{% endif %}

Thanks