CMS Development

mkarsten
トップ投稿者

HUBL limiting dates

解決

I'm working on a blog home landing page using a custom module with HUBL. The basic Hubl is working to pull the 3 most popular blog posts:

<div class="popular-post">
{% set pop_posts = blog_popular_posts('default', 3) %} {% for pop_post in pop_posts %}
<p style="font-size: 16px;"><img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
<a href="{{pop_post.url}}">
{{ pop_post.name }}
</a></p>

 

Now I need to limit it to only include blogs published within the last year from the current date. I started with an if statement, but then found some filtering info, so I've been playing with:

{% set date = 'null' %}
{{date}}
{{date|minus_time(1, 'years') }}

But it's not working, still pulling all time.

 

Any suggestions greatly appreciated!

</div>

0 いいね!
2件の承認済みベストアンサー
Kevin-C
解決策
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

What about increasing our dictionery and limiting the printed content that matches the year criteria?

{% set pop_posts = blog_recent_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today =  local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
  {% for pop_post in pop_posts %}

    {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
    {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

    {% if yearDif <= 0 %} {# if the the yearDif is less than or equal to 0, print #}
      {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
        {% set resCount = resCount + 1 %} {# resCount++ #}
        <p style="font-size: 16px;">
          <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
          <a href="{{pop_post.url}}">
            {{ pop_post.name }}
          </a>
        </p>
      {% endif %} 
    {% endif %} 

  {% endfor %}
</div>

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev

元の投稿で解決策を見る

Kevin-C
解決策
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

I missed that the between_times will return the integer with the value of the difference. So on posts older than 1 year it returns -1 and not 1 like I assumed.

 

Fix:

Change the operator in the first for loop to be ">=" rather than "<=".

 

{% set pop_posts = blog_popular_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today =  local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
  {% for pop_post in pop_posts %}

    {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
    {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

    {% if yearDif >= 0 %} {# if the the yearDif is less than or equal to 0, print #}
      {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
        {% set resCount = resCount + 1 %} {# resCount++ #}
        <p style="font-size: 16px;">
          <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
          <a href="{{pop_post.url}}">
            {{ pop_post.name }}
          </a>
        </p>
      {% endif %} 
    {% endif %} 

  {% endfor %}
</div>
Kevin Cornett - Sr. Solutions Architect @ BridgeRev

元の投稿で解決策を見る

0 いいね!
10件の返信
Kevin-C
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

Hey @mkarsten 

 

Could you try something like this?

<div class="popular-post">
  {% set pop_posts = blog_recent_posts('default', 3) %}
  {% set today =  local_dt %} {# get and assign current date #}
  {% for pop_post in pop_posts %}

  {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
  {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}
  
    {% if yearDif <= 0 %} {# if the the yearDif is less than or equal to 0, print #}

    <p style="font-size: 16px;">
      <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
      <a href="{{pop_post.url}}">
        {{ pop_post.name }}
      </a>
    </p>
  
    {% endif %} 

  {% endfor %}
</div>

 

 

EDIT:

I just noticed that this could very well return less than 3 resualts. I will continue to explore the most effieicent option.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 いいね!
Anonymous
適用対象外

HUBL limiting dates

解決

That might work tho.... if they want to limit it to the past year, they might have to accept that there could be less than 3 posts. 

Kevin-C
解決策
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

What about increasing our dictionery and limiting the printed content that matches the year criteria?

{% set pop_posts = blog_recent_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today =  local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
  {% for pop_post in pop_posts %}

    {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
    {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

    {% if yearDif <= 0 %} {# if the the yearDif is less than or equal to 0, print #}
      {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
        {% set resCount = resCount + 1 %} {# resCount++ #}
        <p style="font-size: 16px;">
          <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
          <a href="{{pop_post.url}}">
            {{ pop_post.name }}
          </a>
        </p>
      {% endif %} 
    {% endif %} 

  {% endfor %}
</div>

 

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
mkarsten
トップ投稿者

HUBL limiting dates

解決

I spoke too soon.:( When I went to double check the blog posts the automation is pulling, it is bringing up the 3 most recent instead of the 3 most popular. When I change the first set to {% set pop_posts = blog_popular_posts('default', 36) %} it then pulls all time instead of only from the last year. Any other suggestions?

0 いいね!
Kevin-C
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

Another note that just occured to me. I'm not sure what order the blog_popular_posts('default', 36) request will return. It might be safe to assume that they'll come in as most – least popular relatively. So they won't be in chronological order if thats the case, but that if statement should still olny print values of a year or less.

 

If thats an issue we will have to look into reordering the pop_posts before running it through the printing logic.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 いいね!
Kevin-C
解決策
名誉エキスパート | Solutions Partner
名誉エキスパート | Solutions Partner

HUBL limiting dates

解決

I missed that the between_times will return the integer with the value of the difference. So on posts older than 1 year it returns -1 and not 1 like I assumed.

 

Fix:

Change the operator in the first for loop to be ">=" rather than "<=".

 

{% set pop_posts = blog_popular_posts('default', 36) %} {# assuming you're publishing 3 posts a month - could be less to reduce load #}
{% set today =  local_dt %} {# get and assign current date #}
{% set resCount = 0 %}

<div class="popular-post">
  {% for pop_post in pop_posts %}

    {% set postDate = pop_post.publish_date %} {# get and assign post publish date per loop #}
    {% set yearDif = today|between_times(postDate, 'years') %} {# compare the date objects to the year - outputs an integer #}

    {% if yearDif >= 0 %} {# if the the yearDif is less than or equal to 0, print #}
      {% if resCount < 3 %} {# if we've matched 3 we can ignore the rest #}
        {% set resCount = resCount + 1 %} {# resCount++ #}
        <p style="font-size: 16px;">
          <img src="{{ pop_post.featured_image }}" width="150" style="padding-right: 15px">
          <a href="{{pop_post.url}}">
            {{ pop_post.name }}
          </a>
        </p>
      {% endif %} 
    {% endif %} 

  {% endfor %}
</div>
Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 いいね!
mkarsten
トップ投稿者

HUBL limiting dates

解決

Checked the stats and that did the trick! Thanks for the quick response.:)

PS: The order looks correct.

mkarsten
トップ投稿者

HUBL limiting dates

解決

Both worked great, but I went with the 2nd suggestion since there was concern about the first showing less than 3 posts. The styling changed a bit to match our main website on a different platform, but otherwise was a straight copy and paste. Appreciated the notes to understand what each step was doing. Thanks so much for the help!!! 

0 いいね!
sharonlicari
コミュニティーマネージャー
コミュニティーマネージャー

HUBL limiting dates

解決

Hey @mkarsten 

 

I will tag a few of our experts to learn from them.   

       

Hey  @alyssamwilie @willsmith @stefen could you please share your knowledge with @mkarsten?

 

Thank you & Happy Holidays

Sharon Emoticono feliz


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




0 いいね!
mkarsten
トップ投稿者

HUBL limiting dates

解決

Thanks for passing it on!