CMS Development

tlow87
参加者

Blog Loop using specific post IDs

解決

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 いいね!
2件の承認済みベストアンサー
JasonRosa
解決策
HubSpot Employee
HubSpot Employee

Blog Loop using specific post IDs

解決

@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! 

元の投稿で解決策を見る

emilushi
解決策
参加者 | Elite Partner
参加者 | Elite Partner

Blog Loop using specific post IDs

解決

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 

元の投稿で解決策を見る

4件の返信
emilushi
解決策
参加者 | Elite Partner
参加者 | Elite Partner

Blog Loop using specific post IDs

解決

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
参加者

Blog Loop using specific post IDs

解決

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 いいね!
JasonRosa
解決策
HubSpot Employee
HubSpot Employee

Blog Loop using specific post IDs

解決

@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
参加者

Blog Loop using specific post IDs

解決

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>