Moving Magento Page Title Above Sidebar: A Comprehensive Guide
Are you struggling to rearrange your Magento category pages? Specifically, do you want to move the page title above the sidebar while maintaining a full-width layout for the main content area? You’re not alone.
The Current Layout Challenge
Let’s first understand the default two-columns-left template structure:
_________________________________
| | |
| | Page Title |
| | |
|(sidebar) | (main content) |
| | |
| | |
|__________|____________________|
In this layout, the sidebar is positioned left of the main content. The page title appears within a narrow column alongside the sidebar, which doesn’t make optimal use of available space.
Your Desired Layout
You want to achieve this:
_________________________________
| |
| Page Title |
|_______________________________|
| | |
|(sidebar) | (main content) |
| | |
| | |
|__________|____________________|
In this version, the page title spans the full width of the page above both the sidebar and main content. The sidebar remains on the left side but now appears below the page title.
Where to Start: Understanding XML Layout
When you look at your Magento category XML layout (catalog_category_view.xml), it might seem challenging to separate the elements:
<layout version="1.0">
<category_view>
<referenceBlock name="page.main.title" after="-" />
<referenceContainer name="content">
<block type="core/text_list" name="category.content" as="content"/>
</referenceContainer>
<referenceBlock name="category.view.container" before="-"/>
</category_view>
</layout>
The page.main.title reference block represents the page title, and category.view.container contains both the sidebar (block type="catalog/navigation") and main content.
The Solution: Rearranging Blocks
To achieve your desired layout, you’ll need to move the page title container above the rest of the content. Here’s how:
- Create a new container for the page title: Add this to your XML layout file:
<referenceContainer name="my.category.title.container">
<block type="core/text" name="category.title" template="page/html/title.phtml"/>
</referenceContainer>
- Move the existing content blocks below the new container: Change the order of references like this:
...
<referenceBlock name="my.category.title.container" />
<referenceContainer name="content">
<block type="core/text_list" name="category.content" as="content"/>
</referenceContainer>
...
- Update your template to use the new block: In
catalog/category/view.phtml, find where you have<h1 class="page-title"><?php echo $this->getPageHeading() ?></h1>. Update it to reference the new container:
<div class="my-category-title">
<?php echo $this->getBlockHtml('category.title') ?>
</div>
- Apply some CSS: To ensure the title spans 100% of the page width, add this to your theme’s CSS file:
.my-category-title {
width: 100%;
background-color: #f8f9fa;
padding: 20px;
}
The Result
After implementing these changes and clearing your cache (cache:flush in the terminal), you should see your category pages displaying the page title fully above the sidebar, with the layout you desired.
Conclusion
Modifying Magento’s default layout requires a good understanding of XML layouts, blocks, containers, and templates. By following this step-by-step guide, you can successfully rearrange your category pages to optimize the use of space.
Remember that these changes are specific to your theme and may require adjustment if you switch themes or update Magento versions. Always test thoroughly before deploying changes to a live production site.
For more advanced customizations like this, consider exploring our comprehensive guides on Magento layout updates at Laravel Company. We provide in-depth tutorials and best practices for Magento development.