Custom monthly archive for blog posts

New Contributor | Gold Partner

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.

Reply
0 Upvotes
2 Replies 2
HubSpot Alumni

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. 

Reply
0 Upvotes
New Contributor | Gold Partner

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!

Reply
0 Upvotes