CMS Development

tlow87
Participant

Blog Loop using specific post IDs

SOLVE

I'm brand new to HubL, and doing my best to grasp how the functions could work together to help me achieve this. I would like to find a simple way to loop in some blog posts, but be able to specify which post IDs I want to display. Below is my terrible attempt at communicating this to you:

<div class="recent-posts-wrapper">
    {% set rec_posts = blog_post_by_id(1491343965, 3677064533, 3676346832) %}
    {% for rec_post in rec_posts %}
        <div class="recent-item span4">
            <a href="{{ rec_post.absolute_url }}"><img src="{{ rec_post.featured_image }}" alt=""></a>
            <a href="{{ rec_post.absolute_url }}"><h4 class="post-title">{{ rec_post.name }}</h4></a>
            <p class="post-summary">{{ rec_post.post_body|striptags|truncatehtml(200, '' , false) }}</p>
        </div>
    {% endfor %}
</div>

As you can see, I'm taking the default Recent Posts HubL module, but want to define the three posts that will display. Could someone point out my flaws so I can become better?!

0 Upvotes
2 Accepted solutions
JasonRosa
Solution
HubSpot Employee
HubSpot Employee

Blog Loop using specific post IDs

SOLVE

@tlow87 you could try something like this: 

 

{% set rec_posts = blog_recent_posts('default', 200) %}
{% for rec_post in rec_posts %}
{% if rec_post.id == 'YOUR BLOG POST ID' %}
<div class="post-title">{{ rec_post.name }}</div>
{{ rec_post.id }}
{% endif %}
{% endfor %}

 

There is a limit of 200 posts that are grabbed in the function so it won't work if the posts you want are not within the most recent 200 posts. If you wanted to pull in multiple post ids you could use an or in the if statement so something like: 

 

{% if rec_post.id == 'YOUR BLOG POST ID 1' or rec_post.id == 'YOUR BLOG POST ID 2'%}

 

Your alternative of doing a topic that is hidden could also work. To hide it from listing you might have to do similar if or unless statements if you list your topics out anywhere so that it doesn't show in the topic listings if that makes sense. 

 

I hope that this helps! 

View solution in original post

emilushi
Solution
Participant | Elite Partner
Participant | Elite Partner

Blog Loop using specific post IDs

SOLVE

I see that this is already marked as resolved but there is a function for doing that:

You can use 

content_by_id OR content_by_ids
{% set contents = content_by_ids([4715624297, 4712623297, 5215624284]) %}
<ul>
{% for content in contents %}
    <li>
        <a href="{{ content.absolute_url }}">{{content.title}}</a>
    </li>
    {% endfor %}
</ul>

https://designers.hubspot.com/docs/hubl/functions#content-by-ids 

View solution in original post

4 Replies 4
emilushi
Solution
Participant | Elite Partner
Participant | Elite Partner

Blog Loop using specific post IDs

SOLVE

I see that this is already marked as resolved but there is a function for doing that:

You can use 

content_by_id OR content_by_ids
{% set contents = content_by_ids([4715624297, 4712623297, 5215624284]) %}
<ul>
{% for content in contents %}
    <li>
        <a href="{{ content.absolute_url }}">{{content.title}}</a>
    </li>
    {% endfor %}
</ul>

https://designers.hubspot.com/docs/hubl/functions#content-by-ids 

tlow87
Participant

Blog Loop using specific post IDs

SOLVE

P.S., my immediate thought would be to place the posts I want displayed into a particular topic, and loop that topic in. But I would want this topic to be invisible to the public. Perhaps a possibility?

0 Upvotes
JasonRosa
Solution
HubSpot Employee
HubSpot Employee

Blog Loop using specific post IDs

SOLVE

@tlow87 you could try something like this: 

 

{% set rec_posts = blog_recent_posts('default', 200) %}
{% for rec_post in rec_posts %}
{% if rec_post.id == 'YOUR BLOG POST ID' %}
<div class="post-title">{{ rec_post.name }}</div>
{{ rec_post.id }}
{% endif %}
{% endfor %}

 

There is a limit of 200 posts that are grabbed in the function so it won't work if the posts you want are not within the most recent 200 posts. If you wanted to pull in multiple post ids you could use an or in the if statement so something like: 

 

{% if rec_post.id == 'YOUR BLOG POST ID 1' or rec_post.id == 'YOUR BLOG POST ID 2'%}

 

Your alternative of doing a topic that is hidden could also work. To hide it from listing you might have to do similar if or unless statements if you list your topics out anywhere so that it doesn't show in the topic listings if that makes sense. 

 

I hope that this helps! 

tlow87
Participant

Blog Loop using specific post IDs

SOLVE

Thanks Jason! That did the trick. Below is the code I used to place the posts into three columns.

 

<div class="recent-posts-wrapper">
{% set rec_posts = blog_recent_posts('default', 200) %}
{% for rec_post in rec_posts %}
{% if rec_post.id == '1491343965' or rec_post.id == '3677064533' or rec_post.id == '3676346832' %}
        <div class="recent-item span4">
            <a href="{{ rec_post.absolute_url }}"><img src="{{ rec_post.featured_image }}" alt=""></a>
            <a href="{{ rec_post.absolute_url }}"><h4 class="post-title">{{ rec_post.name }}</h4></a>
            <p class="post-summary">{{ rec_post.post_body|striptags|truncatehtml(200, '' , false) }}</p>
        </div>
{% endif %}
{% endfor %}
</div>