In category page, I need to show link tags that holds pagination's previous and next links like below:

<link rel="prev" href="http://www.test.com/prod1.html?p=1" />
<link rel="next" href="http://www.test.com/prod1.html?p=3" />

Did some research but no success yet.

I have used head as reference in layout but the problem is, page head content gets loaded before category product collection is loaded because if which total number of products cannot be found.

Please suggest!

Thank you in advance!


2 Answers 2


Try installing this module: https://github.com/drewhunter/SeoPagination

the idea is to listen to this event:


then create your own pager block, assign current product collection and page limit to it so that you have a functional pager that can give you the links to next, previous and current page. Finally you can inject them into the head block like this:

$headBlock = Mage::app()->getLayout()->getBlock('head');
$headBlock->addLinkRel('prev', $nextPageUrl);
$headBlock->addLinkRel('next', $prevPageUrl);

A little too late but for the person who ends here just like I did.

You need to create the controller and view files and place it overhead to make the prev next button appear. If you need more help please check the Reference

Here you go:



namespace ZecraTech\PreviousNext\Block;

use Magento\Framework\View\Element\Template\Context;

class Categories extends \Magento\Framework\View\Element\Template {

    protected $_registry;

    protected $_categoryRepository;

    public function __construct(
        Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Catalog\Model\CategoryRepository $categoryRepository
        $this->_registry = $registry;
        $this->_categoryRepository = $categoryRepository;
        return parent::__construct($context);

    public function getCategoryId() {
        $category = $this->_registry->registry('current_category');
            $category_id = $category->getId();
            $category_id = '';
        return $category_id;

//then we need to know the parent category of the current category
    public function getParentCategoryId() {
        $category = $this->_registry->registry('current_category');
            $category_parent_id = $category->getparent_id();
            $category_parent_id = '';
        return $category_parent_id;

//We loop through all the children of the category
    public function getAllChildrensOfParentCategory() {
        $categoryId = $this->getParentCategoryId();
        $category = $this->_categoryRepository->get($categoryId);
        $subCategories = $category->getChildrenCategories();
        $sub_cat_names = [];
        foreach($subCategories as $subCategory) {
            $sub_cat_names[] = $subCategory->getId();
        return $sub_cat_names;

//Finally we get the category URL
    public function getCategoryUrl($categoryId) {
        $category = $this->_categoryRepository->get($categoryId);
        $category_url = $category->getUrl();
        return $category_url;


<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="page.wrapper">
            <block class="ZecraTech\PreviousNext\Block\Categories" name="next.prev.cat" template="ZecraTech_PreviousNext::category/next-prev.phtml" />
        <move element="next.prev.cat" destination="main.content" before="-" />


<?php $cat_position = array_search($block->getCategoryId(), $block->getAllChildrensOfParentCategory());
if($cat_position > 0) {
    $cat_url_prev = $block->getCategoryUrl($block->getAllChildrensOfParentCategory()[$cat_position - 1]); 
} elseif ($cat_position == 0) { 
    $cat_url_prev = '#';
} else {
    $cat_url_prev = '#';
if($cat_position >= 0 ) {
    if((count($block->getAllChildrensOfParentCategory()) - 1) == $cat_position) {
        $cat_url_next = "#";
    } else {
        $cat_url_next = $block->getCategoryUrl($block->getAllChildrensOfParentCategory()[$cat_position + 1]);
else {
    $cat_url_next = '#';

<div class="next-prev-cat">
   <a href="<?= $cat_url_prev ?>" class="prev-cat">PREVIOUS CATEGORY</a>
   <a href="<?= $cat_url_next ?>" style="float: right;" class="next-cat">NEXT CATEGORY</a>

category page previous next

Your Answer

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

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