CMS Development

johnnyr209
Mitwirkender/Mitwirkende

How to get 3 random blog posts

lösung

How would I generate 3 random Blog posts? Here is what I have so far

 

{% set posts = ????? %}
{% for post in posts %}
<div >
<a href="{{ post.absolute_url }}">
<img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" >
</a>

<h3>
<a href="{{ post.absolute_url }}" class="">{{ post.name }}</a>
</h3>
</div>
{% endfor %}

0 Upvotes
2 Akzeptierte Lösungen
albertsg
Lösung
Ratgeber/-in

How to get 3 random blog posts

lösung

Hi @johnnyr209, you could use the following functions to get blog posts:

https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts 

https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts 

 

And then to make it random, you could use the random filter (https://developers.hubspot.com/docs/cms/hubl/filters#random).

 

Your code could look like this:

{% set posts = blog_recent_posts("default", 30) %}
{% for post in posts|random %}
{% if loop.index <= 3 %}
<div >
<a href="{{ post.absolute_url }}">
<img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" >
</a>

<h3>
<a href="{{ post.absolute_url }}" class="">{{ post.name }}</a>
</h3>
</div>
{% endif %}
{% endfor %}

 

Notice that loop.index will prevent the loop from displaying more than 3 articles.

In the previous example, blog_recent_posts will return 30 elements but it has a limit of 200, so play with that number as you wish (just keep in mind performance, maybe it's no needed to get 200 articles and loop through them all to just show 3)

 



Did my answer help you? Mark it as a solution


Book time with me

You can also connect with me on LinkedIn



Lösung in ursprünglichem Beitrag anzeigen

johnnyr209
Lösung
Mitwirkender/Mitwirkende

How to get 3 random blog posts

lösung

So suffle seams to work instead of random

{% for post in posts|shuffle%}

Lösung in ursprünglichem Beitrag anzeigen

4 Antworten
johnnyr209
Lösung
Mitwirkender/Mitwirkende

How to get 3 random blog posts

lösung

So suffle seams to work instead of random

{% for post in posts|shuffle%}
albertsg
Ratgeber/-in

How to get 3 random blog posts

lösung

Nice catch! I didn't realize |random returns only 1 item 😅 



Did my answer help you? Mark it as a solution


Book time with me

You can also connect with me on LinkedIn



0 Upvotes
johnnyr209
Mitwirkender/Mitwirkende

How to get 3 random blog posts

lösung

The code seams to be pulling a random post but it only displays 1 and not 3.

0 Upvotes
albertsg
Lösung
Ratgeber/-in

How to get 3 random blog posts

lösung

Hi @johnnyr209, you could use the following functions to get blog posts:

https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts 

https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts 

 

And then to make it random, you could use the random filter (https://developers.hubspot.com/docs/cms/hubl/filters#random).

 

Your code could look like this:

{% set posts = blog_recent_posts("default", 30) %}
{% for post in posts|random %}
{% if loop.index <= 3 %}
<div >
<a href="{{ post.absolute_url }}">
<img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" >
</a>

<h3>
<a href="{{ post.absolute_url }}" class="">{{ post.name }}</a>
</h3>
</div>
{% endif %}
{% endfor %}

 

Notice that loop.index will prevent the loop from displaying more than 3 articles.

In the previous example, blog_recent_posts will return 30 elements but it has a limit of 200, so play with that number as you wish (just keep in mind performance, maybe it's no needed to get 200 articles and loop through them all to just show 3)

 



Did my answer help you? Mark it as a solution


Book time with me

You can also connect with me on LinkedIn