In your block file add below code for add pagination:
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Framework\Url\Helper\Data $urlHelper,
array $data = []
) {
$this->_productFactory = $productFactory;
$this->urlHelper = $urlHelper;
parent::__construct($context, $data);
// Get your custom collection here
$collection = $this->getCustomCOllection>getCustomCollection();
$this->setCollection($collection);
}
public function _prepareLayout()
{
parent::_prepareLayout();
if ($this->getCollection()) {
// create pager block for collection
$toolbar = $this->getToolbarBlock();
$pager = $this->getLayout()->createBlock(
'Magento\Theme\Block\Html\Pager', 'list.pager'
)->setCollection(
$this->getCollection() // assign collection to pager
);
$toolbar->setChild('product_list_toolbar_pager', $pager); // set pager block in layout
// called prepare sortable parameters
$collection = $this->getCollection();
// use sortable parameters
$orders = $this->getAvailableOrders();
if ($orders) {
$toolbar->setAvailableOrders($orders);
}
$sort = $this->getSortBy();
if ($sort) {
$toolbar->setDefaultOrder($sort);
}
$dir = $this->getDefaultDirection();
if ($dir) {
$toolbar->setDefaultDirection($dir);
}
$modes = $this->getModes();
if ($modes) {
$toolbar->setModes($modes);
}
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
$this->getCollection()->load();
}
return $this;
}
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
public function getToolbarBlock()
{
$blockName = $this->getToolbarBlockName();
if ($blockName) {
$block = $this->getLayout()->getBlock($blockName);
if ($block) {
return $block;
}
}
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
return $block;
}
public function getMode()
{
return $this->getChildBlock('toolbar')->getCurrentMode();
}
Also define below constantproperty:
inIn phtml file add below code beforbefore and after lising.listing: