问题
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