Magento SEO Breadcrumbs
Magento Breadcrumbs are a bit to be desired, when it comes to consistency and SEO purposes. Product breadcrumbs would shift to whatever the latest category they were added to. So if a product goes on sale in a special “sale” category, then the breadcrumb would be rebuilt and shown on the product page.
This had a negative effect on SEO, in that the products where showing up in sales or even newsletter categories.
We found a post here at StackOverflow and an answer by “Fiasco Labs” got us started on the right path.
The code dropped us down a category, but we still had products that were nested two categories deep. With a few tweaks, we fixed the breadcrumbs for a product to use the very first category it was tagged with. So super long breadcrumbs like “home->newsletter->sales->machines->Super Duper Machine” are now just “home->Big Machines->Super Duper Machine”. As long as the root category is not removed, it should always present a static breadcrumb for subsequent crawls by search engines.
Code:
protected function _toHtml() { $cat_id = ""; if (Mage::registry('current_product')) { $product_id = Mage::registry('current_product')->getId(); $obj = Mage::getModel('catalog/product'); $_product = $obj->load($product_id); // Enter your Product Id in $product_id if ($product_id) { $categoryIds = $_product->getCategoryIds(); $cat_id = $categoryIds[0]; } $category = Mage::getModel('catalog/category')->load($cat_id); $cat_name = $category->getName(); $cat_url = $this->getBaseUrl().$category->getUrlPath(); } if (is_array($this->_crumbs)) { reset($this->_crumbs); $this->_crumbs[key($this->_crumbs)]['first'] = true; end($this->_crumbs); $this->_crumbs[key($this->_crumbs)]['last'] = true; } if($cat_id) { //go through and all cats we don't want. foreach($this->_crumbs as $key=>$value){ if(strpos($key, 'category')!==false): unset($this->_crumbs[$key]); endif; } $this->_crumbs['category'.$cat_id] = array('label'=>$cat_name, 'title'=>'', 'link'=> $cat_url,'first'=>'','last'=>'','readonly'=>''); ksort($this->_crumbs); $home = $this->_crumbs['home']; unset($this->_crumbs['home']); array_unshift($this->_crumbs,$home); } $this->assign('crumbs', $this->_crumbs); return parent::_toHtml(); } |