Get Magento category attribute in frontend
I have created a Category's attribute that I want to use on the frontend. I开发者_如何学编程 tried to access it the same way as we do for products but it doesn't seem to be working. How can I show the custom attribute on the frontend? Any guesses?
Thanks
Try this:
$cat_attr = $this->getCurrentCategory()->getAttributes();
if(array_key_exists('short_description', $cat_attr)):
$_shortDescription=$cat_attr['short_description']->getFrontend()->getValue($_category);
endif;
First thing I would do is do the following code on your category object
print_r($category->debug());
This will show you if that attribute is being loaded. If you don't see your attribute, you can go back to the controller where the object is being loaded and add it to your select by adding one of these lines:
->addAttributeToSelect('your_attribute')
That should load your attribute for you.
I had a custom attribute and got it shown in frontend this way;
$category_id = '10';
$attribute_code = 'category_categorycolor';
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($category);
Quite Simple
<?php foreach ($this->getStoreCategories() as $cat): ?>
<?php $_category=Mage::getModel("catalog/category")->load($cat->getId()); ?>
now you should use getImage method to retrive your img attribute
<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/category/' . $_category->getImage() ?>" />
<?php endforeach ?>
精彩评论