Listing recent posts and dynamically populating tags

SOLVE
ericgordon
Member

This is probably an easy one for seasoned HubSpot devs, but that is NOT ME. 

 

We have pages set up for each team member in the organization—I'm trying to display the three most recent blog posts that are tagged with that member's name. (These will be manually tagged.)

 

The wrinkle is these team member pages are being populated by a separate HubDB, so I need to pass the first and last name of the current member into the tag field. I've tried to set two variables to do this, but no dice. 

 

Current code that does not work:

{% set name_var = "{{ dynamic_page_hubdb_row.first_name }}-{{ dynamic_page_hubdb_row.last_name }}"  %}

{% set tag_posts = blog_recent_tag_posts('XXXXXXXXXX', ['{{ name_var }}'], 3) %}
    <ul>
    {% for tag_post in tag_posts %}
      <li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li>
    {% endfor %}
    </ul>

 

0 Upvotes
1 Accepted solution

Accepted Solutions
Indra
Solution
Top Contributor | Diamond Partner

Hi @ericgordon,

 

Can you try make it without the curly brackets insize the double quotes?

 

So it will look like this:

{% set name_var = dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags ~ '-'  
 ~ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags %}

{# or perhaps make it seperate since you apply filters #}

{% set fist_name = dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags %}
{% set last_name = dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags %}
{# don't forget the seperator #}
{% set name_var = fist_name ~ '-' ~ last_name %}

Bureau Vet

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

12 Replies 12
Indra
Top Contributor | Diamond Partner

Hi @ericgordon,

 

Since you want to filter by authors, you should use another function. So instead of using blog_recent_tag_posts, you should use blog_recent_author_posts.

 

Any options for this can be found in the documentation for hubl functions under blog_recent-author_posts.

 

So your post will look something like this:

{% set author = dynamic_page_hubdb_row.first_name ~ '-' ~ dynamic_page_hubdb_row.last_name  %}
{% set blog_id = 'XXXXXXXXXX' %}

{% set author_posts = blog_recent_author_posts(blog_id, author, 3) %}
<ul>
{% for author_post in author_posts %}
   <li><a href="{{ author_post.absolute_url }}">{{ author_post.name }}</a></li>
{% endfor %}
</ul>

Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
ericgordon
Member

Thanks for the detailed response, Indra. 

 

That’s not quite what I need here though—these blog posts aren’t necessarily authored by a particular team member, but instead are manually tagged with a team member’s name if they’re MENTIONED in the blog post. 

0 Upvotes
Indra
Top Contributor | Diamond Partner

Hi @ericgordon,

 

So for example you have posts setup like this?

  • Blog post from [person 1]
  • Blog post 2 from [person 2]
  • Blog post 3 from [person 1]
  • Blog post 4 from [person 3]

Is that correct?


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
ericgordon
Member

Not quite. 

 

It's more like:

* Person 1 is mentioned in a blog post written by Person 2.

* Person 2 includes the name of Person 1 as a tag in the blog post.

* That blog post appears in a list of Recent Posts on Person1's bio page (which is being populated via a HubDB entry)

0 Upvotes
Indra
Top Contributor | Diamond Partner

Hi @ericgordon,

 

The blog_recent_tag_posts function is set that it will filter by slug. So make sure if you use our code to replace spaces with dashes and make all letters lower case.

 

Replace function: https://developers.hubspot.com/docs/cms/hubl/filters#replace 

Lower filter: https://developers.hubspot.com/docs/cms/hubl/filters#lower 

 

So in your case, try this:

 

{% set name_var = "{{ dynamic_page_hubdb_row.first_name|replace(' ','-')|lower }}-{{ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower }}"  %}

{% set tag_posts = blog_recent_tag_posts('XXXXXXXXXX', ['{{ name_var }}'], 3) %}
    <ul>
    {% for tag_post in tag_posts %}
      <li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li>
    {% endfor %}
    </ul>

 


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
0 Upvotes
ericgordon
Member

Thanks again, Indra

 

Definitely helpful, but the variable still isn't getting passed into the function. I can successfully echo {{ name_var }} (lowercase, with hyphen) pretty much anywhere but it doesn't seem to get inserted into the blog_recent_tag_posts string.

 

{% set name_var = "{{ dynamic_page_hubdb_row.first_name|replace(' ','-')|lower }}-{{ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower }}" %}

{% set tag_posts = blog_recent_tag_posts('XXXXXXXXXX', '{{ name_var }}', 3) %} (THIS DOESN'T WORK)

{{ name_var }} (THIS WORKS)

<ul>
{% for tag_post in tag_posts %}
<li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li>
{% endfor %}
</ul>

 

0 Upvotes
Indra
Top Contributor | Diamond Partner

Hi @ericgordon,

 

Ahh my bad. You need to remove the curly brackets for it. So your code will be like this:

{% set name_var = "{{ dynamic_page_hubdb_row.first_name|replace(' ','-')|lower }}-{{ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower }}"  %}
{% set blog_id = 'XXXXXXXXXX' %}

{% set tag_posts = blog_recent_tag_posts( blog_id , name_var , 3) %}
    <ul>
    {% for tag_post in tag_posts %}
      <li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li>
    {% endfor %}
    </ul>

 


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
ericgordon
Member

Thanks yet again. I feel like that SHOULD have worked, but no luck. It really does seem that blog_recent_tag_posts doesn't like a variable for the tag name.

 

This does not work:

{% set tag_posts = blog_recent_tag_posts( blog_id , name_var , 3) %} 

But manually inserting the tag does:

{% set tag_posts = blog_recent_tag_posts( blog_id , 'jane-doe' , 3) %} 

 

0 Upvotes
Indra
Top Contributor | Diamond Partner

Hi @ericgordon,

 

This should indeed work.

 

Do you have a sample page where you have an output of {{ name_var }} or your full code?

You can also try and apply some extra filters like safe, striptags.


Bureau Vet

Did my post solve your question? Help the community by marking it as a solution
0 Upvotes
ericgordon
Member

Still nothing. I'm afraid. Tried both safe and striptags.

 

This does not work:

{% set name_var = "{{ dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags }}-{{ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags }}" %} 

{% set blog_id = 'XXXXXXXXXXX' %} 

{% set tag_posts = blog_recent_tag_posts( blog_id , name_var , 3) %} 
  <ul> 
    {% for tag_post in tag_posts %} 
	  <li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li> 
    {% endfor %} 
</ul>

 

But redefining the name_var variable manually DOES.

{% set name_var = "{{ dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags }}-{{ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags }}" %} 

{% set name_var = "firstname-lastname" %} 

{% set blog_id = 'XXXXXXXXXXX' %} 

{% set tag_posts = blog_recent_tag_posts( blog_id , name_var , 3) %} 
  <ul> 
    {% for tag_post in tag_posts %} 
	  <li><a href="{{ tag_post.absolute_url }}">{{ tag_post.name }}</a></li> 
    {% endfor %} 
</ul>

 

0 Upvotes
Indra
Solution
Top Contributor | Diamond Partner

Hi @ericgordon,

 

Can you try make it without the curly brackets insize the double quotes?

 

So it will look like this:

{% set name_var = dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags ~ '-'  
 ~ dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags %}

{# or perhaps make it seperate since you apply filters #}

{% set fist_name = dynamic_page_hubdb_row.first_name|replace(' ','-')|lower|striptags %}
{% set last_name = dynamic_page_hubdb_row.last_name|replace(' ','-')|lower|striptags %}
{# don't forget the seperator #}
{% set name_var = fist_name ~ '-' ~ last_name %}

Bureau Vet

Did my post solve your question? Help the community by marking it as a solution

View solution in original post

ericgordon
Member

That did it! Thanks for chipping away at a solution, Indra.

 

Cheers,

E.