Magento – Magento 2: get Nextpage, Lastpage Url in list.phtml file

list.phtmlmagento2pagertoolbar

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.

Best Answer

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()
        );
Related Topic