I am working on a Magento 2.1.7 shop and I have created a child-theme of Magento Blank.

I need to add class "paginated" to the body tag if the products category pages display pagination.

I have copied the pager.phtml file to app/design/frontend/Company/Theme/Magento_Theme/templates/html/, so that I can safely overwrite the defaults.

In this file, there is an if statement that "decides" when the pagination is displayed:

<?php if ($block->getLastPageNum()>1): ?>
 // Pagination code here
<?php endif; ?>

Is there anything I can add inside this if statement that would add class "paginated" to the body tag?

2 Answers 2


If you have access of block class then you can do it like this,

use Magento\Framework\View\Page\Config as PageConfig;

protected $pageConfig;

public function __construct(
    pageConfig $pageConfig
) {
    $this->pageConfig = $pageConfig;

Now you can use this like,


If you don't have access of block class then you can do it by objectManager (not recommended)

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  • In what file shall I put this second snippet of code? Jan 15, 2018 at 13:13
  • I have access to the entire project, but I do not know where the block class is. I am new to Magento. Jan 15, 2018 at 13:19
  • For testing purpose you can put objectManager code into <?php if ($block->getLastPageNum()>1): ?> // Pagination code here <?php endif; ?>
    – Keyur Shah
    Jan 15, 2018 at 13:32
  • I have added it to pager.phtml file and it ruins it: the code appears in the browser. Jan 15, 2018 at 13:37
  • appears in the browser means ?
    – Keyur Shah
    Jan 15, 2018 at 13:40

There is a better solution to the problem this question is trying to solve: Do not display a “per page” select box, if products are not paginated.

Your Answer

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

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