Displaying Magento Pager Only at the Bottom of Product List
In Magento, the pager is typically included within the list.phtml template alongside other elements like sorting options. However, you can separate the pager to appear only at the bottom of your product list by using a custom module and overriding the relevant templates.
Understanding the Current Setup
When you create a category view in Magento, it uses the list.phtml template located at app/design/frontend/base/default/template/catalog/product/list.phtml. This template includes all necessary elements such as sorting options ($_productCollection->setOrder()), the product list itself ($this->getProductListHtml()), and the pager ($this->getPagerHtml()).
The issue you’re facing is that the <?php echo $this->getPagerHtml() ?> code isn’t working because it’s being interpreted as a string rather than PHP code. To resolve this, we’ll need to create an observer to add the pager separately using a custom module.
Creating a Custom Module
-
First, create a new module folder structure:
app/code/local/YourVendor/YourModule. Replace “YourVendor” and “YourModule” with your desired namespace and module name. -
Inside
etc/config.xml, define your module:
<config>
<modules>
<YourVendor_YourModule>
<active>true</active>
<codePool>local</codePool>
</YourVendor_YourModule>
</modules>
</config>
- Create a file
app/code/local/YourVendor/YourModule/etc/config.xmland add the following observer configuration:
<config>
<global>
<models>
<yourmodule>
<class>YourVendor_YourModule_Model</class>
</yourmodule>
</models>
</global>
<frontend>
<events>
<catalog_category_view>
<observers>
<add_pager_to_list>
<type>singleton</type>
<class>yourmodule/observer</class>
<method>addPagerToList</method>
</add_pager_to_list>
</observers>
</catalog_category_view>
</events>
</frontend>
</config>
- Create the observer file
app/code/local/YourVendor/YourModule/Model/Observer.php:
class YourVendor_YourModule_Model_Observer
{
public function addPagerToList(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if ($block instanceof Mage_Catalog_Block_Product_List) {
echo $block->getPagerHtml();
}
}
}
- Clear the Magento cache (
rm -rf var/cache/*) and refresh your category page.
Modifying the List.phtml Template
To remove the pager from the top of the list, you can modify the list.phtml file found in your current theme directory (e.g., app/design/frontend/rwd/default/template/catalog/product/list.phtml). Locate this line:
<?php echo $this->getPagerHtml() ?>
Comment it out or remove it.
Conclusion
By following these steps, you’ve successfully created a custom module that adds the pager to your product list separately from other elements. The pager will now appear only at the bottom of your category pages.
Remember, this approach requires knowledge of Magento’s observer system and basic PHP coding. If you’re new to Magento development, consider referring to our guide on creating custom modules for a more detailed walkthrough.
This method ensures that your product list remains clean and uncluttered, with the pager appearing exactly where you want it.