0

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?

0

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,

$this->pageConfig->addBodyClass('test');

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

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$pageConfig=$objectManager->create('Magento\Framework\View\Page\Config');
$pageConfig->addBodyClass('test');
  • In what file shall I put this second snippet of code? – Razvan Zamfir Jan 15 '18 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. – Razvan Zamfir Jan 15 '18 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 '18 at 13:32
  • I have added it to pager.phtml file and it ruins it: the code appears in the browser. – Razvan Zamfir Jan 15 '18 at 13:37
  • appears in the browser means ? – Keyur Shah Jan 15 '18 at 13:40
0

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, privacy policy and cookie policy

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