Archived topic
Archive seperated by year
5 replies · Started by Yannick on May 5, 2022
Dear support team,
I am trying to rebuild an archive layout that is grouped by the year of publication. I have added the page that I am trying to rebuild in the private information field. I want to have a heading for each year and the posts from the year below. Everything on one page.
2022
- Post 1
- Post 2
2021
- Post 3
- Post 4
etc.
Is it possible to build something like that with elements or by manipualting the archive query? I searched the forum but wasnt able to find a similar question.
Thank you very much in advance!
Kind regards
Yannick
Hi Yannick,
Good timing :)
We just released GenearateBlocks 1.5 which includes a brand new Query Loop block.
https://generateblocks.com/generateblocks-1-5-dynamic-data-query-loops-image-blocks/
With that block, you can add posts to a static page with lots of parameter options.
In your case, you can add before and after parameter to each Query Loop block, for example, if you want the query loop block to show posts of 2021, you can set it to before: Jan 1, 2022 and after Jan 1, 2021:
https://www.screencast.com/t/qPP5pg7NK7p
Let me know if you have any questions regarding this.
Hi Ying,
thats great timing :)
I will try to figure out using the Query Loop block. I am actually using another Query-Loop-Plugin at the moment for some other layouts.
But if I use the Query Loop block I have to add a new block to the page every year. Thats exactly that kind of task that I will forget about next year :D That is why I wanted to use an archive page an generate the headings for earch year dynamically. You have any idea how to achive that?
Kind regards
Yannick
PS: Does the new Query Loop Block has an "Load More" aka. AJAX pagination option?
That is why I wanted to use an archive page an generate the headings for earch year dynamically. You have any idea how to achive that?
I don't think so Unfortuanly, it will require custom development which is out of the scope of the forum.
PS: Does the new Query Loop Block has an “Load More” aka. AJAX pagination option?
No, it doesn't at this moment.
Hi Ying,
I figured out a very simple way to add yearly headlines to an archive page. I creates a custom archive template and made the following change to the default template. Here is the code if someelse has the same question:
<?php
$date_old = '';
while ( have_posts() ) :
the_post();
/**
* Start of changes
*/
$date_new = get_the_time("Y");
if ( $date_old != $date_new ) : // run the check on $date_old and $date_new, and output accordingly
?> <h2 class="archive-year generate-columns"> <?php echo $date_new?> </h2> <?php
endif;
$date_old = $date_new; // update $date_old
/**
* End of changes
*/
generate_do_template_part( 'archive' );
endwhile;
If you use a column layout you need to customise your CSS, so the headline takes a full line and is not part of the grid.
Unfortunetly I am not able to make if work with AJAX loading. But it is not that important for me.
Great! Thanks for sharing the code :)