Retrieve Url of category on Magento when creating a custom menu

大憨熊 提交于 2019-12-10 12:19:33

问题


I am trying to create a custom drop-down-menu with only few specific categories and their respective subcategories. So far I managed to retrieve the Names of the subcategories but links won't work.

I also need to make the main category retrieve its own name and URL automaticly in case it is changed on the back-end of Magento. In this case the category id is 265.

The website I am working on is www.personalproducts4u.co.uk

<li class="eight"><a href="<?php echo $this->getUrl() ?>index.php/contacts">Hotel Products</a>
 <?php $children = Mage::getModel('catalog/category')->getCategories(265); ?>
 <ul>
 <?php foreach ($children as $category): ?>
 <li>
 <a href="<?php echo $category->getUrl ?>">
 <?php echo $category->getName(); ?>
 </a>
 </li>
 <?php endforeach; ?>
 </ul>
 </li>

回答1:


The problem is that the $children collection is of type Varien_Data_Tree_Node_Collection and its elements respectively are of type Varien_Data_Tree_Node. Calling getUrl() on them will return null, they are not Mage_Catalog_Model_Category objects. However, you can retrieve their request path (url) by calling:

$category->getRequestPath();

Alternatively you can load the category object by calling:

$cat = Mage::getModel('catalog/category')->load($category->getEntityId());

And then use the $cat->getUrl() call. This loading will add an extra overhead though.



来源:https://stackoverflow.com/questions/14586189/retrieve-url-of-category-on-magento-when-creating-a-custom-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!