<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to get 3 random blog posts in CMS Development</title>
    <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600950#M27984</link>
    <description>&lt;P&gt;Nice catch! I didn't realize |random returns only 1 item&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Mar 2022 14:51:16 GMT</pubDate>
    <dc:creator>albertsg</dc:creator>
    <dc:date>2022-03-24T14:51:16Z</dc:date>
    <item>
      <title>How to get 3 random blog posts</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/599419#M27909</link>
      <description>&lt;P&gt;How would I generate 3 random Blog posts? Here is what I have so far&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;{% set posts = ????? %}&lt;BR /&gt;{% for post in posts %}&lt;BR /&gt;&amp;lt;div &amp;gt;&lt;BR /&gt;&amp;lt;a href="{{ post.absolute_url }}"&amp;gt;&lt;BR /&gt;&amp;lt;img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" &amp;gt;&lt;BR /&gt;&amp;lt;/a&amp;gt;&lt;/P&gt;&lt;P&gt;&amp;lt;h3&amp;gt;&lt;BR /&gt;&amp;lt;a href="{{ post.absolute_url }}" class=""&amp;gt;{{ post.name }}&amp;lt;/a&amp;gt;&lt;BR /&gt;&amp;lt;/h3&amp;gt;&lt;BR /&gt;&amp;lt;/div&amp;gt;&lt;BR /&gt;{% endfor %}&lt;/P&gt;</description>
      <pubDate>Tue, 22 Mar 2022 16:42:28 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/599419#M27909</guid>
      <dc:creator>johnnyr209</dc:creator>
      <dc:date>2022-03-22T16:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to get 3 random blog posts</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600234#M27938</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/71073"&gt;@johnnyr209&lt;/a&gt;, you could use the following functions to get blog posts:&lt;/P&gt;&lt;P&gt;-&amp;nbsp;&lt;A href="https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts" target="_blank"&gt;https://developers.hubspot.com/docs/cms/hubl/functions#blog-recent-posts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;-&amp;nbsp;&lt;A href="https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts" target="_blank"&gt;https://developers.hubspot.com/docs/cms/hubl/functions#blog-popular-posts&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then to make it random, you could use the &lt;STRONG&gt;random&lt;/STRONG&gt; filter (&lt;A href="https://developers.hubspot.com/docs/cms/hubl/filters#random" target="_blank"&gt;https://developers.hubspot.com/docs/cms/hubl/filters#random&lt;/A&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code could look like this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{% set posts = blog_recent_posts("default", 30) %}
{% for post in posts|random %}
{% if loop.index &amp;lt;= 3 %}
&amp;lt;div &amp;gt;
&amp;lt;a href="{{ post.absolute_url }}"&amp;gt;
&amp;lt;img data-src="{{ resize_image_url(post.featured_image, 350 ) }}" alt="{{ post.name }}" title="{{ post.name }}" &amp;gt;
&amp;lt;/a&amp;gt;

&amp;lt;h3&amp;gt;
&amp;lt;a href="{{ post.absolute_url }}" class=""&amp;gt;{{ post.name }}&amp;lt;/a&amp;gt;
&amp;lt;/h3&amp;gt;
&amp;lt;/div&amp;gt;
{% endif %}
{% endfor %}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Notice that&lt;STRONG&gt; loop.index&lt;/STRONG&gt; will prevent the loop from displaying more than 3 articles.&lt;/P&gt;&lt;P&gt;In the previous example, &lt;STRONG&gt;blog_recent_posts&lt;/STRONG&gt; 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)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Mar 2022 17:02:50 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600234#M27938</guid>
      <dc:creator>albertsg</dc:creator>
      <dc:date>2022-03-23T17:02:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to get 3 random blog posts</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600911#M27981</link>
      <description>&lt;P&gt;The code seams to be pulling a random post but it only displays 1 and not 3.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2022 13:53:04 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600911#M27981</guid>
      <dc:creator>johnnyr209</dc:creator>
      <dc:date>2022-03-24T13:53:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to get 3 random blog posts</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600938#M27983</link>
      <description>&lt;P&gt;So suffle seams to work instead of random&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{% for post in posts|shuffle%}&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 24 Mar 2022 14:29:38 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600938#M27983</guid>
      <dc:creator>johnnyr209</dc:creator>
      <dc:date>2022-03-24T14:29:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to get 3 random blog posts</title>
      <link>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600950#M27984</link>
      <description>&lt;P&gt;Nice catch! I didn't realize |random returns only 1 item&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Mar 2022 14:51:16 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/CMS-Development/How-to-get-3-random-blog-posts/m-p/600950#M27984</guid>
      <dc:creator>albertsg</dc:creator>
      <dc:date>2022-03-24T14:51:16Z</dc:date>
    </item>
  </channel>
</rss>

