CMS Development

pmullin
Member

Can i add date title to my blog "all blog post listing" page?

https://blog.cppinvest.com/all

 

Want a title for "2018" posts and "2017" posts etc to break up this list making it easier to read.

0 Upvotes
2 Replies 2
Jsum
Key Advisor

Can i add date title to my blog "all blog post listing" page?

@pmullin I figured this out.

 

I tried a number of things to see if this was possible and I came up with something based on @tjoyce's post here, based on my post here. This is actually a pretty cool use case.

 

{# -- the question refers to the simple_list_page which is in the 'else' section below -- #}
{% if not simple_list_page %}
    Iterated post markup for regular listing                            
{% else %}

{# -- counter variable -- - create outside of loop -- #} {% set counter = 1 %}
{# -- last blog in loop's year -- - create outside of loop -- #} {% set last_post_year = '' %}
{# -- contents loop -- #} {% for content in contents %} {# -- current blog in loop's year -- - inside loop -- #} {% set blog_year = content.created|datetimeformat('%Y') %} {# -- if counter is equal to 1 -- #}
{% if counter == 1 %}
{# -- print current blogs year -- - the first is always true so logic is simple -- #} <h3>{{ blog_year }}</h3> {# -- if current post's year equals year in last posts variable -- } - change counter to 1 --#} {% if last_post_year == blog_year %} {# -- change counter to 0 -- #} {% set counter = 0 %} {% else %} {# -- else change to 1, change being operative --#} {% set counter == 1 %} {% endif %} {# -- change current blog post's year to last_post_year variable for next item in loop --#} {% set last_post_year = blog_year %} {% endif %} <h2 class="post-listing-simple"><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
{% endfor %}
{% endif %}

Outside your contents loop you can create to variables, a counter and  holder for the previous post in the loop's year.

 

If the counter equals 1 then print the year. then compare the current post's year to the last post's year. If they are equal then change the counter to 1. This causes each year to print out once, above the first post that as that year. This is assuming your sorting by date, but can be ascending or descending. 

 

Need help? Hire Us Here

pmullin
Member

Can i add date title to my blog "all blog post listing" page?

I am not the best at coding... can this be done over a call?

0 Upvotes