CMS Development

MattOnce
Contributor

Display recent posts from tag where tag is a variable

SOLVE

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-pos...

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>

0 Upvotes
1 Accepted solution
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Display recent posts from tag where tag is a variable

SOLVE

as far as i can understand error caused by missing colon (:) after the variable categorytag.

Try this code.

<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>
Digital Marketing & Inbound Expert In Growth Hacking Technology

View solution in original post

4 Replies 4
himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Display recent posts from tag where tag is a variable

SOLVE

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:

<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>
Digital Marketing & Inbound Expert In Growth Hacking Technology
MattOnce
Contributor

Display recent posts from tag where tag is a variable

SOLVE

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?

0 Upvotes
himanshurauthan
Solution
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Display recent posts from tag where tag is a variable

SOLVE

as far as i can understand error caused by missing colon (:) after the variable categorytag.

Try this code.

<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>
Digital Marketing & Inbound Expert In Growth Hacking Technology
MattOnce
Contributor

Display recent posts from tag where tag is a variable

SOLVE

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.