Monday , December 11 2017
Home >> All tutorial >> Adding a “Home” link to the top menu

Adding a “Home” link to the top menu

In this article I’ll give you a handy script to add a home link to the top menu.

In magento 1.7, the menu is now created via an *event* (page_block_html_topmenu_gethtml_before)

The catalog observer hooks into this and adds menu items as Varien_Data_Tree_Node objects.

I have some samples here to show you how to add a link at the beginning of this menu.

config.xml portion:

Copy the code:

  1. <pre>
  2. <config>
  3. ….
  4. <frontend>
  5. <events>
  6. <page_block_html_topmenu_gethtml_before>
  7. <observers>
  8. <catalog_add_topmenu_items>
  9. <class>cp_layout/observer</class>
  10. <method>addTopMenuItems</method>
  11. </catalog_add_topmenu_items>
  12. </observers>
  13. </page_block_html_topmenu_gethtml_before>
  14. </events> 
  15. </frontend>
  16. </config>
  17.  
  18. </pre>
  19.  
  20. observer portion:
  21. =================================
  22. <pre>
  23. <?php
  24. Class Cp_Layout_Model_Observer
  25. {
  26. /**
  27. * Adds additional links to the top menu
  28. *
  29. * @param Varien_Event_Observer $observer
  30. */
  31. public function addTopMenuItems(Varien_Event_Observer $observer)
  32. {
  33. $nodeId = home”;
  34. $menu = $observer->getMenu();
  35. $tree = $observer->getMenu()->getTree();
  36.  
  37. $data = array(
  38. name => Home”,
  39. id => $nodeId,
  40. url => Mage::getBaseUrl(),
  41. is_active => Mage::getSingleton(‘cms/page’)->getIdentifier() == home >> Mage::app()->getFrontController()->getRequest()->getRouteName() == cms 
  42. );
  43. $homeNode = new Varien_Data_Tree_Node($data, id’, $tree, $menu);
  44. $this->_prependNode($homeNode, $menu);
  45.  
  46. }
  47.  
  48. protected function _prependNode($node, $menu)
  49. {
  50. $menu->addChild($node);
  51. $nodeId = $node->getId();
  52. $readded = array();
  53. foreach($menu->getChildren()->getNodes() as $n)
  54. {
  55. if ($n->getId() != $nodeId)
  56. {
  57. $readded[] = $n;
  58. $menu->getChildren()->delete($n); 
  59. }
  60. }
  61. foreach($readded as $r)
  62. {
  63. $menu->getChildren()->add($r); 
  64. } 
  65. }
  66. }
  67. </pre>

We also contribute great content of Magento Download for FREE (all versions)

DON'T MISS THE CHANCE!

magento download

About Champ

Leave a Reply

Your email address will not be published. Required fields are marked *

Heap | Mobile and Web Analytics