In this instance, it is used at the end of a blog post template to pull recent, related posts to the blog post the user has just finished reading.
I’d like to add different tag(s) to the square brackets depending on the tags that have been applied to the blog page the recent_tag_post statement is used in.
For example, if the blog post has been tagged ‘red’ and ‘blue’, the blog_recent_tag_posts statement would display all posts tagged ‘red’ or ‘blue’, whereas if the post had just been tagged ‘green’, it would just pull ‘green’ posts.
The statement needs to be able to establish this just from content.tag_list however, we have 5 different tags so it seems silly to have 25 conditionals for 2 tags, 125 conditional checks for a potential 3 tags
I have tried:
- Using variables to apply strings of multiple tags (in the format: ’ “red”, “blue”, “green” ’ for example)
- Putting a for-loop in the square brackets to iterate through content.tag_list (not sure if there’s a way to ensure the for-loop is done first?)
But neither have ended in the desired result.
Has anyone got experience with this / a similar issue?
Final edit: Thanks very much for getting back to me - I have managed to achieve this, at least enough for my use-case. The proposed solution may work for some, but didn’t for me for some reason - it fell under the ‘Using variables to apply string(s) of multiple tags’ method I’d tried and had no luck with.
The solution I used is perhaps a bit cumbersome, but was far more efficient than writing out every possible combination of 1-5 different tags being selected.
I get the length of the tag list array:
{% set NumberOfTags = content.tag_list|length %}
Then use if/elif/else conditionals to account for the length of the array to say:
I totally forgot about the third parameter, couldn’t you use the |length directly as the third parameter? Maybe like this;
{% set NumberOfTags = content.tag_list|length|int %}
You could also use a for loop to iterate through the tags and format and add the values to an array.
{% set tags = [] %}
{% for tag in content.tag_list %}
// You could add formatting here like `tag.slug|replace('', '')`
{% do tags.append(tag.slug) %}
{% endfor %}