# Display recent posts from tag where tag is a variable

**URL:** <https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012>\
**Category:** CMS Development\
**Created:** [June 9, 2023, 7:10am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012 "2023-06-09T07:10:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MattOnce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mattonce/32/87988_2.png) [@MattOnce](https://community.hubspot.com/u/MattOnce)\
**Post date:** [June 9, 2023, 7:10am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012/1 "2023-06-09T07:10:50Z")

</div>

This is the same issue as this post, which got no solution:  
[https://community.hubspot.com/t5/CMS-Development/Use-variable-for-topic-inside-blog-recent-topic-posts/m-p/194456](https://community.hubspot.com/t5/CMS-Development/Use-variable-for-topic-inside-blog-recent-topic-posts/m-p/194456)  
At the bottom of a blog post, I want to show the 5 most recent posts that share the same specific tag as the current post (excluding the post iteself).  
I have code that chooses one of the post’s tags to list in the breadcrumbs of the page, based on some logic, mimicking a category.  
It’s this specific tag that I want to use to get the recent posts. I don’t know what it is, as it varies post to post.  
I have created a variable, “CategoryTag”, that is the same value as the breadcrumb tag.  
{{ CategoryTag }} displays the strong of text perfectly.  
The issue is that when I use this variable inside the blog\_recent\_tag\_posts function, it does not work.  
Can anyone offer some insight on this issue?  
Thanks

A simplified version of my code:  
\<h2\>Related topics from {{ CategoryTag }}\</h2\>  
\<ul\>  
{% set rec\_posts = blog\_recent\_tag\_posts(“default”, CategoryTag, 6) %}  
{% set i = 0 %}  
{% for rec\_post in rec\_posts %}  
{% if content.absolute\_url != rec\_post.absolute\_url %}  
{% if i \< 5 %}  
\<li class=“post-title”\>\<a href=“{{ rec\_post.absolute\_url }}”\>{{ rec\_post.name }}\</a\>\</li\>  
{% set i = i + 1 %}  
{% endif %}  
{% endif %}  
{% endfor %}  
\</ul\>

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [June 9, 2023, 8:16am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012/2 "2023-06-09T08:16:03Z")

</div>

Hello @MattOnce  
This function expects a string as its first parameter, You can fix this by using the {{ string }} tag to convert the variable to a string before passing it to the function. Here is the code with the fix:

```lia-code-sample
<h2>Related topics from {{ CategoryTag }}</h2>
<ul>
{% set rec_posts = blog_recent_tag_posts("default", {{ CategoryTag }}, 6) %}
{% set i = 0 %}
{% for rec_post in rec_posts %}
{% if content.absolute_url != rec_post.absolute_url %}
{% if i < 5 %}
<li class="post-title"><a href="{{ rec_post.absolute_url }}">{{ rec_post.name }}</a></li>
{% set i = i + 1 %}
{% endif %}
{% endif %}
{% endfor %}
</ul>

```

---

<div class="post-metadata">

**Author:** ![MattOnce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mattonce/32/87988_2.png) [@MattOnce](https://community.hubspot.com/u/MattOnce)\
**Post date:** [June 9, 2023, 8:32am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012/3 "2023-06-09T08:32:11Z")

</div>

Hi @himanshurauthan , thank you for the response.

I had tried this previuosly, and after trying it again now, I received the same error:  
Error parsing ‘[blog\_recent\_tag\_posts(“default”, {{ categoryTag }}, 6)]’: syntax error, encountered ‘}’, expected ‘:’

Do you know how to overcome this issue?

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [June 9, 2023, 8:43am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012/4 "2023-06-09T08:43:37Z")

</div>

as far as i can understand error caused by missing colon (🙂 after the variable categorytag.  
Try this code.

```lia-code-sample
<h2>Related topics from {{ CategoryTag }}</h2>
<ul>
{% set tag = CategoryTag|string %}
{% set rec_posts = blog_recent_tag_posts("default", tag, 6) %}
{% set i = 0 %}
{% for rec_post in rec_posts %}
 {% if content.absolute_url != rec_post.absolute_url %}
 {% if i < 5 %}
 <li class="post-title"><a href="{{ rec_post.absolute_url }}">{{ rec_post.name }}</a></li>
 {% set i = i + 1 %}
 {% endif %}
 {% endif %}
{% endfor %}
</ul>

```

---

<div class="post-metadata">

**Author:** ![MattOnce](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mattonce/32/87988_2.png) [@MattOnce](https://community.hubspot.com/u/MattOnce)\
**Post date:** [June 9, 2023, 9:35am UTC](https://community.hubspot.com/t/display-recent-posts-from-tag-where-tag-is-a-variable/83012/5 "2023-06-09T09:35:52Z")

</div>

Thanks again for your input.

I don’t know why, but further investigation revealed that it was in fact working, but the function was not generating any results from that tag.  
What seems to have worked now, is hyphenating the tag variable, like this:  
{% set Tag = CategoryTag | lower | replace(’ ', ‘-’) %}  
Now it works, which is great. I’ll have to keep an eye out for further issues brought on by the hyphenation.
