I am working on custom module. How can i use default magento pagination with my custom collection and also set custom limits on that?


get collection for that

public function getNews()
      //get values of current page
        $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
    //get values of current limit
        $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 1;

        $newsCollection = $this->newscollectionFactory->create();
        return $newsCollection;

Add pagination

protected function _prepareLayout()

    if ($this->getNews()) {
        $pager = $this->getLayout()->createBlock(
        $this->setChild('pager', $pager);
    return $this;

adding the child block

public function getPagerHtml()
    return $this->getChildHtml('pager');

in phtml file

    <?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?php echo $block->getPagerHtml(); ?></div>
    <?php endif ?>


  • 1
    Pssst: either $this->getRequest()->getParam('p') ?: 1 or, better yet, $this->getRequest()->getParam('p', 1). – nevvermind Aug 8 '16 at 13:39
  • I get element with id already exists error for whatever I try to name the pager block I'm creating using your code. – LM_Fielding Dec 13 '16 at 14:40
  • @LM_Fielding not faced this issue yet but try deleting var/cache and var/generation folder – Qaisar Satti Dec 14 '16 at 6:08
  • I was trying to add it for a toolbar at the top and at the bottom, so it was exactly correct. – LM_Fielding Dec 15 '16 at 9:07
  • 1
    @LM_Fielding you are adding it twice that is causing the problem. – Qaisar Satti Dec 15 '16 at 9:49

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.