How to get Next page, Last Page url in magento 2?

I have try in template file with below codes,

$_productCollection = $block->getLoadedProductCollection();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
<?php if (!$_productCollection->count()): ?>
    <div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div>
<?php else: ?>
    <?php echo $block->getToolbarHtml() ?>
    <?php echo $block->getAdditionalHtml() ?>
        $viewMode = 'grid';
        $image = 'category_page_grid';
        $showDescription = false;
        $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;

    $pos = $block->getPositioned();
    <?php //echo $this->getLayout()->createBlock("Manadev\LayeredNavigation\Blocks\Navigation")->setTemplate("Manadev_LayeredNavigation::navigation.phtml")->toHtml();
        $pagerBlock = $this->getLayout()->createBlock("Magento\Theme\Block\Html\Pager");
       echo $pagerBlock->getLastPageNum().' test';exit;

        echo $pagerBlock->getNextPageUrl().' sssss';exit;

How to get next and last page url in magento 2 custom template file. only getting current page url.

Is there missing for override pager block?

I have to pass nextpage url value inside my custom code. Also how to check how many page for current category listing?

Any help would be appericiated.

1 Answer 1


The right methods are getPreviousPageUrl() and getNextPageUrl():

public function getPreviousPageUrl()
    return $this->getPageUrl($this->getCollection()->getCurPage(-1));

public function getNextPageUrl()
    return $this->getPageUrl($this->getCollection()->getCurPage(+1));

However, in your case I'm pretty sure it won't help because you don't assign any collection to your pager.

I suggest you have a look at how it is done on the product list toolbar:


  • its not possible to get next and last page url in list page? Jun 30, 2016 at 8:05
  • @Rakesh what do you mean ? I didn't say it was not possible I said you were missing the part of the code where you should have assigned the collection to your pager Jun 30, 2016 at 8:06
  • plz give me suggestion how to add collection in list.phtml file? Jun 30, 2016 at 8:07
  • @Rakesh I posted the code already!!!! Jun 30, 2016 at 8:08
  • plz check updated code and i have echo for nextpage url but not getting Jun 30, 2016 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.