Hi Everyone,
I could use some assistance. I’ve implemented the following code to create a filter and display all tags for this blog category. However, I’m facing an issue where all tags are showing instead of just the selected ones on the detail recipe page. https://agoodlife.co.za/…/gluten-free-vegan-sugar…
Code:
{% if blog.tags.size > 0 %}
**{% for tag in blog.tags %}**
{{ tag }}
{% endfor %}
{% endif %}
It will be much appreciated if someone can assist, I just need to update the {% for tag in blog.tags %} to only show selected tags on the post.
Thank you!
Add this code in your previous code
{% if blog.tags.size > 0 %}
{% assign selected_tags = "selected_tag1, selected_tag2, selected_tag3" | split: ", " %} {# Replace "selected_tag1, selected_tag2, selected_tag3" with your selected tags #}
{% for tag in blog.tags %}
{% if tag | split: " " | intersect: selected_tags | size > 0 %}
- {{ tag }}
{% endif %}
{% endfor %}
{% endif %}
I attached one note add in this code show please read care full and than add this code in your code
{# Replace "selected_tag1, selected_tag2, selected_tag3" with your selected tags #}
If you read this interstation, you simply remove this line in the code
tim_1
April 19, 2024, 3:59pm
3
I’d modify your code like this:
{% assign selected = "tag1,tag2,tag3" | split : "," %}
{% assign has_tags = false %}
{% capture output %}
{% for tag in blog.tags %}
{% if selected contains tag %}
{% assign has_tags = true %}
- {{ tag }}
{% endif %}
{% endfor %}
{% endcapture %}
{% if has_tags %}
{{ output }}
{% endif %}
You may consider fetching the list of selected tags from theme/section settings or Blog metafields.
PS: I do not think Shopify has intersect array filter (unfortunately).
Also {# #} are not comments in Shopify (unfortunately again), but you can use {% # comment content %}.
1 Like
Thanks a mil! this worked