Pooja_mulik Posted November 23, 2022 Share Posted November 23, 2022 Can we display year in descending order for pagination? Link to comment Share on other sites More sharing options...
Pooja_mulik Posted November 23, 2022 Author Share Posted November 23, 2022 Hi all, Quick question as I haven't found anything from my Googling. I have a blogs on the site which utilizes pagination and I want to display years in descending order(i.e.: 2022, 2021,2020,.....) instead of 1,2,3... I'm not sure how to go about this so any direction would be helpful. Thanks Link to comment Share on other sites More sharing options...
Jan Romero Posted November 23, 2022 Share Posted November 23, 2022 I don’t think ProcessWire has this built in. Do you have gaps that you need to skip or maybe show as inactive? If not you can just find the oldest and newest page and iterate between them. Otherwise I suggest custom SQL to efficiently group by something like year(pages.published). I think RockFinder can also group things, might want to check that out. Link to comment Share on other sites More sharing options...
millipedia Posted November 24, 2022 Share Posted November 24, 2022 Grouping your posts by year is a different use case than pagination which is just showing a fixed number of posts per page. For example you might have a year with dozens of posts that still need to be paginated. When we've needed to do this in the past we've used URL segments to pull out a year and then build a selector from that. We can then use the results from that query for pagination if need be. The code below uses a URL like /blog/by-year/2020 ; you could easily build a subnav that has the years in you need to filter by. Obviously you'd need to update the code to match your fields but hopefully it will point you in the right direction. <?php namespace ProcessWire; $filter_title=''; // Do we have url parameters? if ($input->urlSegment1 ) { // in this example we only filter by year if($input->urlSegment1 =='by-year'){ $year=(int)$input->urlSegment2; $year_end=$year . '-12-31'; $year_start=$year . '-01-01'; if($year > 2000 && $year < 2050){ // not really santizing but I just don't want to encourage any Widdecombe of the Week behaviour. $filter_title=$year; } $results = $pages->find("template=news_item, publish_from<=".$year_end.",publish_from>=".$year_start.",limit=12, sort=publish_from"); }else{ $filter_title="Sorry - unknown filter."; } }else{ $results = $pages->find("template=news_item, limit=12, sort=-publish_from"); } if($filter_title){ echo '<h2>' . $filter_title .'</h2>'; } if($results && $results->count){ $pagination = $results->renderPager(); echo '<div class="news-list">'; foreach($results as $result) { echo '<div class="news-item">'; echo '<div class="news-title"><a href="'.$result->url . '">' . $result->title .'</a></div>'; echo '<div class="news-date">' . $result->publish_from .'</div>'; echo '<div class="news-summary">' . $result->summary .'</div>'; echo '</div>'; } echo "</div>"; echo '<div class="text-center">' . $pagination .'</div>'; }else{ echo '<div class="news-list">No results found.</div>'; } 1 1 Link to comment Share on other sites More sharing options...
mulikp Posted November 27, 2022 Share Posted November 27, 2022 Thanks @millipedia @Jan Romero Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now