a-ok Posted January 12, 2017 Posted January 12, 2017 Is there a way to return the next page in the pagination (as a link) only instead of rendering out the full pagination? <?php echo $results->renderPager(array( 'nextItemLabel' => "Next", 'previousItemLabel' => "Prev", 'listMarkup' => "<ul class='MarkupPagerNav'>{out}</ul>", 'itemMarkup' => "<li class='{class}'>{out}</li>", 'linkMarkup' => "<a href='{url}'><span>{out}</span></a>" ));
horst Posted January 12, 2017 Posted January 12, 2017 7 minutes ago, oma said: Have you tried: <?php echo $results->renderPager(array( 'nextItemLabel' => "Next", 'previousItemLabel' => "Prev", 'listMarkup' => "", 'itemMarkup' => "", 'linkMarkup' => "<a href='{url}'><span>{out}</span></a>" )); ?
Mike Rockett Posted January 12, 2017 Posted January 12, 2017 23 minutes ago, horst said: ? Would you murder me if I told you I was doing this via CSS on my blog? ? I'm sure I searched for this, but couldn't find what I was looking for - gonna try this out.
a-ok Posted January 12, 2017 Author Posted January 12, 2017 3 hours ago, horst said: ? Unfortunately I did try this but it returns nothing. `linkMarkup` is also used for the numbers too (not just prev/next).
Robin S Posted January 12, 2017 Posted January 12, 2017 As @Mike Rockett says, the quick and dirty way is to use CSS: .MarkupPagerNav li { display:none; } .MarkupPagerNav .MarkupPagerNavPrevious, .MarkupPagerNav .MarkupPagerNavNext { display:block; } For a PHP solution, here's what I use for prev/next pagination of news summaries. I think it's based on some code shared here in the forums but I can't remember where. In my case I wanted greyed-out prev/next links when on the first/last page. <?php $limit = 5; $news = $pages->find("template=news_item, post_date<=today, limit=$limit, sort=sort"); $total_pages = ceil($news->getTotal() / $limit); // output news summaries here // pagination if($total_pages > 1): $current_url = $page->url; if($input->urlSegmentsStr) $current_url .= $input->urlSegmentsStr . '/'; ?> <div class="pagination"> <div class="prev-page"> <?php if($input->pageNum > 2): ?> <a rel="prev" href="<?= $current_url . $config->pageNumUrlPrefix . ($input->pageNum - 1) ?>">Prev page</a> <?php elseif($input->pageNum === 2): ?> <a rel="prev" href="<?= $current_url ?>">Prev page</a> <?php else: ?> Prev page <?php endif; ?> </div> <div class="centre-text">Page <?= $input->pageNum ?> of <?= $total_pages ?></div> <div class="next-page"> <?php if($input->pageNum < $total_pages): ?> <a rel="next" href="<?= $current_url . $config->pageNumUrlPrefix . ($input->pageNum + 1) ?>">Next page</a> <?php else: ?> Next page <?php endif; ?> </div> </div> <?php endif; ?> 4
abdus Posted January 12, 2017 Posted January 12, 2017 There's $page->next() and $page->prev() methods available that returns sibling pages matching an optional selector. You can use $next = $page->next("published>$page->published, template=$page->template"); $prev = $page->prev("published<$page->published, template=$page->template"); Then you can echo their urls and build your own simple navigation. See: https://processwire.com/api/ref/page/https://processwire.com/api/ref/page/next/https://processwire.com/api/ref/page/prev/ 1
a-ok Posted January 16, 2017 Author Posted January 16, 2017 Thanks all! @Robin S I went with your PHP solution... like it avoid hiding via CSS.
isellsoap Posted June 19, 2020 Posted June 19, 2020 Hi @a-ok, i solved this with this code: $news = $page->children("limit=5"); $pager = $modules->get('MarkupPagerNav'); // this is required in order for getUrl to work // see https://processwire.com/api/ref/markup-pager-nav/get-u-r-l/ $pager->render($news); $paginationMarkup = ''; if ($news->hasPrevPagination() || $news->hasNextPagination()) { $paginationMarkup .= '<ul>'; if ($news->hasPrevPagination()) { $paginationMarkup .= "<li><a href='{$pager->getUrl($pager->pageNum - 1)}'>prev</a></li>"; } if ($news->hasNextPagination()) { $paginationMarkup .= "<li><a href='{$pager->getUrl($pager->pageNum + 1)}'>next</a></li>"; } $paginationMarkup .= '</ul>'; } echo $paginationMarkup; 1
Jan Romero Posted July 4, 2024 Posted July 4, 2024 May I just say… this won’t work: <?php $lastPageNum = ceil($posts->getTotal() / $limit); ?> <a href="<?=page()->url($lastPageNum)?>">Go to the last page</a> Because ceil() always returns a float. This also means you may get subtle bugs when using === to compare it. Personally I detest having to divide and ceil() and cast to int myself, so my proposal is to add this functionality to the core. It would be something like this: public function getTotalPages() { return (int)ceil($this->getTotal() / $this->getLimit()); } Or should it be called getLastPageNum()? Might make a PR later…
Jan Romero Posted July 7, 2024 Posted July 7, 2024 (edited) Here’s another edge case: 0 results. So really you gotta do this, if you presume an empty pagination page to exist: (int)ceil(($this->getTotal() ?: 1) / $this->getLimit()); Edited July 7, 2024 by Jan Romero
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