Magento Breadcrumbs: Removing Slashes and Incorporating into Extensions
Hello developers! Today we’re going to tackle two common challenges involving Magento breadcrumbs. Let’s dive right in.
Removing the Slash from Breadcrumbs
By default, Magento includes a slash (/) between the breadcrumb items. If you want to remove this slash for a cleaner look â like “Home Page” instead of “Home / Page” â follow these steps:
-
Locate the
vendor/magento/module-theme/view/frontend/templates/html/breadcrumbs.phtmlfile. -
Find this line:
if ($count > 0) {
echo '<ul class="breadcrumbs"><li><span>' . __('Home') . '</span></li>';
}
- Replace it with the following code to remove the slash:
if ($count > 0) {
$html = '<ul class="breadcrumbs"><li><span>' . __('Home') . '</span></li>';
foreach ($breadcrumbs as $_breadcrumb) {
$html .= '<li><span>' . __('' . $_breadcrumb['label'] . ' ') . '</span></li>';
}
echo $html;
}
Now, your breadcrumbs should appear without the slash between items. If you still face issues, ensure that there are no overriding templates in your theme.
Inserting Breadcrumbs into Extension Pages
To insert breadcrumbs into an extension’s page, follow these steps:
-
Navigate to the XML layout file for your custom page (usually located under
app/design/frontend/Vendor/Theme/Magento_YourExtension/layout). -
Add or update the breadcrumb block in the XML:
<referenceBlock name="breadcrumbs" template="Magento_Cms::block/breadcrumbs.phtml"/>
-
Create a new PHTML file for your custom page (if it doesn’t exist already) under
app/design/frontend/Vendor/Theme/Magento_YourExtension/templates. Name the file appropriately, e.g.,yourpage.phtml. -
Within this PHTML file, ensure you have the following PHP code to render the breadcrumbs:
<?php
/**
* @var $block \Magento\Theme\Block\Html\Breadcrumbs
*/
$breadcrumbs = $block->getBreadcrumbs();
if (!is_array($breadcrumbs) || !$breadcrumbs) {
return;
}
?>
- Place the breadcrumb block in your page layout:
<referenceContainer name="content">
<block type="Magento\Theme\Block\Html\Breadcrumbs" template="Magento_Cms::block/breadcrumbs.phtml"/>
</referenceContainer>
Remember to replace Vendor and Theme with your actual vendor and theme names.
Conclusion
There you have it! With these steps, you should now be able to remove the slash from Magento breadcrumbs and incorporate them into custom extension pages. Always ensure you’re working in a development environment first and test thoroughly before deploying changes.
For more detailed guides on Magento development and extensions, I highly recommend exploring our Magento Development category at Laravel Company for comprehensive tutorials and best practices.
Until next time, happy coding!