Magento: Adding Pagination and Setting Column Number Simultaneously
In this guide, we’ll explore how to add pagination while setting a specific column count for your product listings in Magento 1.7. We’ll provide clear instructions and relevant code examples to help you achieve this functionality.
Understanding the Current Setup
You’re currently using the following layout XML code to display products with pagination:
<reference name="content">
<block type="catalog/product_list" name="home"
template="catalog/product/list.phtml">
<action method="setCategoryId"><category_id>137</category_id></action>
<block type="catalog/product_list_toolbar"
name="product_list_toolbar"
template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
This code is inserted inside CMS > Pages > Design and works as expected for displaying products from category ID 137 with pagination.
The Challenge: Setting Column Count
You’ve discovered that you can set the column count using the following content code:
{{block type="catalog/product_list" column_count="5"
category_id="2" template="catalog/product/list.phtml"}}
When placed in CMS > Pages > Content, this code effectively sets the number of products displayed per row to 5. However, it lacks pagination functionality.
The Solution: Combining Pagination and Column Count
To solve this issue, we need to merge the layout XML (for pagination) with the content block (for column count). Here’s how you can achieve this:
- First, let’s modify the existing layout XML code in CMS > Pages > Design as follows:
<reference name="content">
<block type="catalog/product_list" name="home"
template="catalog/product/list.phtml">
<action method="setCategoryId"><category_id>2</category_id></action>
<block type="catalog/product_list_toolbar"
name="product_list_toolbar"
as="toolbar">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
Note the changes:
- We’ve updated
category_idto 2. - Added
as="toolbar"to the product list toolbar block.
- Next, we’ll modify the CMS > Pages > Content as follows:
{{block type="catalog/product_list" template="catalog/product/list.phtml"
name="home" column_count="5" category_id="2"}}
{{block type="core/template" template="page/html/pager.phtml"
name="product_list_toolbar_pager" pager_css_class="items per page"}}
{{block type="catalog/product_list_toolbar" name="product_list_toolbar"}}
In this content block:
- We first define the product listing block with a name (“home”) and set the desired column count (5) and category ID (2).
- We then include the pager template using a separate block.
- Finally, we include the toolbar block.
Why This Works
The key to making both pagination and column count work simultaneously is the proper use of layout XML and content blocks. By defining the product list block in the layout XML with a name (“home”) and including it in the content block with the desired settings (column_count), you effectively merge the two functionalities.
Conclusion
In this guide, we explored how to add pagination while setting a specific column count for your Magento product listings. We discussed the current setup, the challenge of merging these two features, and provided a comprehensive solution involving both layout XML and content blocks.
Remember that proper configuration of layout XML and careful use of content blocks are crucial for achieving desired output in Magento’s CMS pages.
For further reference on Magento development best practices and advanced techniques, consider visiting Laravel Company to access our extensive resources and guides tailored for Magento developers.