Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

List posts group by year

  • #4915
    Avatar photo[anonymous]

    Hello,

    First of all, I wish you a Happy New Year, success in your projects, good health and happiness.

    I’m in the process of creating my own theme and visually grouping the posts with the year of creation at the top of the list.

    Do you have a tip for getting the year to the top of the list ? Via the {{#getPosts}} helper, {{#each posts}} or other ?

    Thanks for your advices

    Here, example of the desired rendering:

    <h2>2020</h2>
    Lorem ipsum dolor sit amet
    - December 18, 2020
    Sed ut perspiciatis unde omnis
    - April 02, 2020
    Sed ut perspiciatis unde omnis
    - January 13, 2020
    <h2>2019</h2&gt
    Lorem ipsum dolor sit amet
    - Spetember 15, 2019
    Sed ut perspiciatis unde omnis
    - Mars 23, 2019
    #4916
    Avatar photoBob

    I’m afraid it’s not easy to do, there is no ready helper for this, you can try to use tags (mark them as hidden) to group posts by years.

    #4917
    Avatar photo[anonymous]

    Hello Bob,

    Thank you for your answer, very good tip. I will do like that.

    Thank you for your help.

    #4918
    Avatar photo[anonymous]

    Hello,
    I took your advice and coded this:

    I’m looking for a way to loop through each year if an article exists, I haven’t found a solution in the docs. Because this solution requires me to manually implement the code for each year.

    Do you have an idea ?

    Thank you so much, Nicolas

    <h2>2020</h2>
        {{! loop that generates a list of posts for 2020 }}
        {{#getPostsByTags (concatenate "count=999&tag_as=slug&tags=2020&excluded=0&offset=0&orderby=modifiedAt&ordering=asc")}} 
            <ul class="posts-list">
                <li class="posts-list-item">
                    {{! post title }}
                    {{title}}
                    {{! /post title }}
    
                    {{! post creation date }}
                    <time datetime="{{date createdAt 'YYYY-MM-DDTHH:mm'}}">
                        {{date createdAt}}
                    </time>
                    {{! /post creation date }}
                
            
        {{/getPostsByTags}}
        {{! /loop that generates list of posts }}
    
        <h2>2019</h2>
        {{! loop that generates a list of posts for 2019 }}
        {{#getPostsByTags (concatenate "count=999&tag_as=slug&tags=2019&excluded=0&offset=0&orderby=modifiedAt&ordering=asc")}} 
            <ul class="posts-list">
                <li class="posts-list-item">
                    {{! post title }}
                    {{title}}
                    {{! /post title }}
    
                    {{! post creation date }}
                    <time datetime="{{date createdAt 'YYYY-MM-DDTHH:mm'}}">
                        {{date createdAt}}
                    </time>
                    {{! /post creation date }}
                
            
        {{/getPostsByTags}}
        {{! /loop that generates list of posts }}
    #4927
    Avatar photo[anonymous]

    Hi @Nicolas,

    You can do it much easier by custom helpers:

    1) Please create in helpers.js the following helpers:

    let themeHelpers = {
        yearsInArchive: function(startYear) {
            let years = [];
            let currentYear = new Date().getFullYear();
    
            for (let i = currentYear; i >= startYear; i--) {
            	years.push(i);
            }
    
            return years;
        },
        getPostsByYear: function(posts, year) {
        	return posts.filter(post => new Date(post.createdAt).getFullYear() === year);
        }
    };
    
    module.exports = themeHelpers;

    2) Then use the following code in your theme:

    <h1>YEARS</h1>
    
      {{#each (yearsInArchive 2016) }}
    1. <h2>{{ this }}</h2> {{#if (getPostsByYear @website.contentStructure.posts this)}}
        {{#each (getPostsByYear @website.contentStructure.posts this)}}
      1. {{title}} - {{date createdAt 'YYYY-MM-DDTHH:mm'}}
      2. {{/each}}
      {{else}} No posts {{/if}}
    2. {{/each}}

    It should render posts separated by years – if you change the year in the yearsInArchive helper – you can change the start year for your archive. The final year will be always the current year 🙂

    #4932
    Avatar photo[anonymous]

    Hi Tomasz,

    I wish you a happy new year, success in your projects, happiness and all that goes with it.

    Thanks for the “Helper”, it works perfectly.

    I will continue to read the documentation to learn more.

    See you soon, Nicolas