CMS Development

mattias
Participant

Custom monthly archive for blog posts

This issue came up when I needed to implement a monthly archive listing functionality on a customer's blog. The issue at hand is that only some archive pages list every post that was published during that month, but others do not. Those who don't usually list only 1 post. I don't think this is due to the code, since this is very inconsistent behavior, but I will add my code either way.

 

{% set month_day_list =[] %}
{% set month_list =[] %}
{% set year_list =[] %}
{% set posts = blog_recent_posts('default', 250) %}

<select onchange="if(this.value) window.location.href=this.value">
  <option value="-1">Choose month</option>
  {% for post in posts %}
  {% set publish_month_day = post.publish_date | datetimeformat('%Y/%m') %}

  {% set month_day_list = month_day_list + publish_month_day %}
{% set publish_month = post.publish_date | datetimeformat('%B') %}
{% set month_list = month_list | unique + publish_month %} {% if loop.last %} {# I honestly don't understand why or how this if statement works btw #} {% for month in month_day_list|unique %} <option value="{{ group.absolute_url }}/archive/{{ month }}"> {{ month_list[loop.index - 1] | title }} {{ month | truncate(4, True, '') }} </option> {% endfor %} {% endif %} {% endfor %} </select>

I do have to be honest and say that I found this code snippet on this forum, and I can't quite wrap my head around it entirely. But since this doesn't look like a coding error (the links are generating just fine, the listing pages are not), I'm thinking this might be something I'm missing in the way HubSpot is handling blog posts?

 

I will not bore you with posting my listing page markup, it's the usual stuff. I'm just wondering if I'm missing something about publishing dates or whatever? All posts were imported from Wordpress, but they all have their "correct" publishing dates and they're all visible when browsing.

0 Upvotes
3 Replies 3
cbarley
HubSpot Alumni
HubSpot Alumni

Custom monthly archive for blog posts

hi @mattias , to be transparent, it's pretty tough to diagnose the issue solely based on the code. If you have screenshots and links to templates that show what exactly is going on, and why there aren't the correct number of posts, I'd be happy to lend another set of eyes on this. If you also have the other post that you can link here that you're referencing, that would be great too. 

0 Upvotes
mattias
Participant

Custom monthly archive for blog posts

Thanks for your reply @cbarley! I will try to add more information here, but as you say, it's very difficult to diagnose either way. I'm still completely at a loss.

 

The original code (which I've modified a bit) is found here.

 

And the listing page is for example this one, which is supposed to list posts from December 2018. As you can see, it only lists a single post although there are multiple with publish date in December - e.g. this post is listed as a Dec '18 post, but does not appear in the archive listing.

 

The posts were all added to Hubspot in February this year, but the publish date is set to their original dates (all posts were imported from Wordpress). I've tried to adjust the date and republish the post, but to no avail.

 

Any and all ideas on further troubleshooting this are highly appreciated!

0 Upvotes
Jsum
Key Advisor

Custom monthly archive for blog posts

I ran into the same issue. I found this post and tried to use the code. 

 

The issue with the code is that it only adds a month to the list of months if it is unique. July 2021 and July 2020 are the same without the year, so it doesn't work.

I rewrote it. I know this post is old, but I'm sure someone will find this helpful.

 

 

 

        {# get blog posts #}
        {% set posts = blog_recent_posts('default', 250) %}
        
        {# initiate data list variable (outside of posts forloop) #}
        {%- set month_year_list = [] -%}
        
        {# posts forloop #}
        {%- for post in posts -%}
        
          {# entry tuple: year, month (name), month (number) #}
          {%- set entry = (post.publish_date|datetimeformat('%Y'), post.publish_date|datetimeformat('%B'), post.publish_date|datetimeformat('%m')) -%}
        
          {# unless a tuple matching 'entry' in month_year_list, in given iteration of posts loop, append to month_year_list #}
          {%- unless entry in month_year_list -%}
            
            {# using a new variable to append allow. we never use new variable, this is a worker to pack the original month_year_list #}
            {%- set month_year_list2 = month_year_list.append(entry) -%}
          {%- endunless -%}
        {%- endfor -%}
        
        
        <ul>
          {# outside of posts loop. loop month_year_list tuples #}
          {%- for item in month_year_list -%}
            <li>
              {# extract tuple contents by index #}
              <a href="{{ group.absolute_url }}/archive/{{item[0]}}/{{ item[2] }}">
                {{ item[1] }} {{ item[0] }}
              </a>
            </li>  
          {%- endfor -%}
        </ul>

 

 

 

0 Upvotes