如何在Magento 2中隐藏工具栏?现有代码方案是否最优及其他方案咨询
Hey there! Let’s break down your question about hiding the toolbar in Magento 2, evaluate the code solution you’re referring to, and explore other practical options.
First off, since you didn’t share the exact code, I’ll assume it’s one of the two most common approaches used for this task:
Case 1: Layout XML Modification
If your code looks like this (to remove the toolbar entirely):
<referenceBlock name="product_list_toolbar" remove="true"/>
Or this (to hide it without removing the block from the layout):
<referenceBlock name="product_list_toolbar" display="false"/>
Then yes, this is a correct solution. Magento’s layout system natively supports remove and display attributes to control block visibility. Just make sure you place this code in the right layout file—usually app/design/frontend/[YourVendor]/[YourTheme]/Magento_Catalog/layout/catalog_category_view.xml (for category pages) or catalog_product_list.xml (for all product list pages) to target the right pages.
Case 2: CSS Hiding
If your code uses CSS like this:
.toolbar { display: none !important; }
This is also correct, but it’s a front-end-only fix—the toolbar still gets rendered in the backend, it just doesn’t show up to users.
It depends on your specific needs:
- If you don’t need any toolbar functionality at all (no pagination, sorting, or item count controls), using
remove="true"in layout XML is the best option. It prevents Magento from rendering the toolbar block entirely, which saves unnecessary server processing and reduces page weight for better performance. - If you might need to restore the toolbar later or want to keep it on some pages (like search results),
display="false"or CSS hiding is more flexible—you can revert the change quickly without rewriting code. - CSS hiding is the simplest to implement, but it’s not the most performant choice, as the toolbar’s DOM elements still load in the background.
Here are a few alternative methods depending on your use case:
1. Conditional Hiding with a Plugin
If you only want to hide the toolbar on specific pages (e.g., a single category), create a plugin to intercept the toolbar’s rendering:
<?php namespace YourVendor\YourModule\Plugin; use Magento\Catalog\Block\Product\ProductList\Toolbar; class HideToolbarConditionally { public function aroundToHtml(Toolbar $subject, \Closure $proceed) { // Example: Hide only for category ID 123 $categoryId = $subject->getRequest()->getParam('id'); if ($categoryId == 123) { return ''; } // Render the toolbar normally for other pages return $proceed(); } }
This gives you granular control over where the toolbar appears without affecting all pages.
2. Check for Theme Configuration Options
Many third-party Magento themes include built-in settings to toggle the toolbar on or off. Head to Stores > Configuration > [Your Theme] > Catalog and look for options like "Show Product List Toolbar"—if it exists, you can hide the toolbar with just a few clicks, no code required.
3. Customize the Toolbar Template
If you want to hide only parts of the toolbar (not the whole thing), copy the default toolbar template from vendor/magento/module-catalog/view/frontend/templates/product/list/toolbar.phtml to your theme’s directory (app/design/frontend/[YourVendor]/[YourTheme]/Magento_Catalog/templates/product/list/toolbar.phtml). Then comment out or delete the sections you don’t want to display, like the pagination or sorting controls.
内容的提问来源于stack exchange,提问作者Vinay Singh

