Menu
Web Design | SEO | Website Development

UK Based Web Agency, Magento & Wordpress Specialist (Cranleigh | Surrey)

How to force Magento into showing full breadcrumbs (Now with Google Rich Snippets)

By In Advice, Platforms, Website Development On 08/03/2012


This is a mod to force Magento into showing the full breadcrumbs page structure. Which helps for easy navigation and most importantly for search engine spidering.

  • get your breadcrumbs.phtml file from public_html/app/design/frontend/default/YOUR-TEMPLATE/template/page/html/
  • Add this code to the top of the code:
<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>
  • So that you end up with something like this:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
?>

<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>">
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<?php echo $this->htmlEscape($_crumbInfo['label']) ?>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

Update 29/05/2013

If you want to combine the above with Google Rich Snippets, i.e. so that the breadcrumbs show up as Rich Snippets and you are still forcing Magento into showing full breadcrumbs, then use this code instead:

<?php
if ($product = Mage::registry('current_product')) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel('catalog/category')->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry('current_category')) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}

if($category) {
if($path = $category->getPath()) {
$path = explode('/', $path);
$crumbs = array('home' => array('label' => 'Home',
'title' => 'Home',
'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
'first' => true,
'last' => false
));
for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel('catalog/category')->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
'title' => $cur_category->getName(),
'link' => $cur_category->getUrl(),
'first' => false,
'last' => false
);
}
}
$crumbs['current'] = array('label' => $lastCrumbName,
'title' => '',
'link' => '',
'first' => false,
'last' => true
);
}
}
?>

<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
<ul xmlns:v="http://rdf.data-vocabulary.org/#">
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" typeof="v:Breadcrumb">
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->htmlEscape($_crumbInfo['title']) ?>" rel="v:url" property="v:title"><?php echo $this->htmlEscape($_crumbInfo['label']) ?></a>
<?php elseif($_crumbInfo['last']): ?>
<strong><?php echo $this->htmlEscape($_crumbInfo['label']) ?></strong>
<?php else: ?>
<span rel="v:child" property="v:title"><strong><?php echo $this->htmlEscape($_crumbInfo['label']) . ' for sale' ?></strong></span>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span> > </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

To test the richsnippets are working use Google Testing Tool: http://www.google.com/webmasters/tools/richsnippets

If you want to use all the other currently available rich snippets the plugin I recommend is: http://grafischdirect.com/magento-extension-google-rich-snippets (€79.96) as a combination of the above code and this plugin will give you complete Rich Snippet coverage. FYI at the time of writing this Google isn’t showing product images in RS but it won’t be long… In any event the Grafisch Direct plugin has already got the code to included for when the images when they become available.


About the Author

Dan Vince

Yet another Web Development Professional

Related Posts

25 Comments

  • mass 6 YEARS AGO

    That is really great! Dose it work if the product belongs to both of rootcategory and subcategory?

    Reply
  • Dan Vince 6 YEARS AGO

    Hi Mass,No problem... Yes it does... but it defaults to structure of whichever root cat or sub cat has the lowest ID number.Cheers

    Reply
  • mass 6 YEARS AGO

    I appreciate your reply, Danny. I do understand. My root cat has lower ID no. than sub cat. That is why sub cat disappeared from breadcrumbs on my product page. Is there any solutions for it at this moment?? If any, please kindly let me know.

    Reply
  • Dan Vince 6 YEARS AGO

    Howdy, alas i don't know the solution to that i'm afraid.

    Reply
    • mass 6 YEARS AGO

      Thanks. I will try to find a solution.

      Reply
      • Dan Vince 6 YEARS AGO

        Nice, if you figure it out, please let me know.

        Reply
  • Walter 6 YEARS AGO

    Thanks, Works great for my website..

    Reply
  • Ben 6 YEARS AGO

    Hello,Yes, this code is fantastic! The only issue I just found is that before with the original file when you clicked on product reviews the breadcrumbs would end in / Product Reviews and now mine doesn't.I removed the code and it shows up again. But then of course the full category trail does not.Would there be any fix for this? Thank you!

    Reply
  • Karl 5 YEARS AGO

    Hi, thanks this is great. Whould it be possible to add google rich snippets? like: http://inchoo.net/ecommerce/magento/google-rich-snippets-in-magento/Regards,

    Reply
  • Arch 5 YEARS AGO

    This will affect on performance of the site as it loading category collection on page load.

    Reply
  • Rob 5 YEARS AGO

    I have tried this, but it doesn't appear to be looping. I only get 1 breadcrumb line, and it has the first selected category (based on my category order).Any thoughts?Thanks

    Reply
  • David 5 YEARS AGO

    I used this code on a multi-store setup. But for some reason it only works on the store using the first root category. Other stores that use different root categories do not show breadcrumbs. Is there a way this could be edited to use different root categories?

    Reply
  • tudor 4 YEARS AGO

    Nothing affected. Went perfectly. Thank you so much for the Google rich snippets code. Have been searching for this code for some time now but only run into errors . Either the links wouldn't work, or couldn't get rid of the Home section.

    Reply
  • Sean 4 YEARS AGO

    Can you tell me if the breadcrumbs (Now with Google Rich Snippets) will show the image of the product or is there additional code needed? Thanks.

    Reply
    • Dan Vince 4 YEARS AGO

      Hi,This is just for the breadcrumbs. For Product images rich snippets I recommend a plugin like: http://grafischdirect.com/magento-extension-google-rich-snippets

      Reply
  • coltivazione indoor 4 YEARS AGO

    Hello, good article, however i have noticed that after I have copied your code, on the cms page the breadcrumbs are wrong. Instead of displaying " Home + title's page" now I have "Home > Default category". Is there a way to correct it? Thank you Andrea

    Reply
    • Dan Vince 4 YEARS AGO

      Hi Andrea, Hmmmm i'm not sure... it works for me on all the magento sites i run.

      Reply
  • Paul Simmons 4 YEARS AGO

    Hello, very nice tutorial. I have developed an extension that displays breadcrumbs and other product related information in SERPS. Let me know how do you rate this one? http://www.fmeaddons.com/magento/rich-snippets-extension.html

    Reply
  • ratnesh choudahary 4 YEARS AGO

    Thank you your code is really good i help me a lot. now it is showing the right way of bredcrumbs.

    Reply
  • Brendon 3 YEARS AGO

    Hi Dan,I have a problem with the Bread crumb not showing the entire trail on our product page of our website. I have implemented your code and it doesn't work.Is there anything else I can do? I would appreciate any other tips.RegardsBrendon

    Reply
  • Perry 3 YEARS AGO

    Breadcrumbs have been recently changes, now the homepage and category links are not clickable. I Don't like at all but still I'm using them plus product rich snippets. I didn't configured that myself, I just used an extension, this one https://amasty.com/magento-google-rich-snippets.html

    Reply
  • Niono 3 YEARS AGO

    On product pages it does not show sub-categories... One more magento-snippet / tutorial which doesn't work from scratch, that seems to be cool because everybody does it ...

    Reply
  • Thet Thet Mar 10 MONTHS AGO

    Thanks a lot for your help. This helped me a lot.

    Reply

Leave a comment