1

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

I have try in template file with below codes,

   <?php
$_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() ?>
    <?php
        $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

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:

        $pagerBlock->setAvailableLimit($this->getAvailableLimit());

        $pagerBlock->setUseContainer(
            false
        )->setShowPerPage(
            false
        )->setShowAmounts(
            false
        )->setFrameLength(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setJump(
            $this->_scopeConfig->getValue(
                'design/pagination/pagination_frame_skip',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            )
        )->setLimit(
            $this->getLimit()
        )->setCollection(
            $this->getCollection()
        );
  • its not possible to get next and last page url in list page? – Rakesh Jesadiya Jun 30 '16 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 – Raphael at Digital Pianism Jun 30 '16 at 8:06
  • plz give me suggestion how to add collection in list.phtml file? – Rakesh Jesadiya Jun 30 '16 at 8:07
  • @Rakesh I posted the code already!!!! – Raphael at Digital Pianism Jun 30 '16 at 8:08
  • plz check updated code and i have echo for nextpage url but not getting – Rakesh Jesadiya Jun 30 '16 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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