简体   繁体   中英

Displaying Current Sub-Category name on Magento Product page

I currently working on a magento store where i need to display the name and url of current subcategory of the product in the product page. Till now i have come up with the following code.

<?php $categories = $_product->getCategoryIds(); ?>

                <?php 


                foreach($categories as $k => $_category_id):

                $_category = Mage::getModel('catalog/category')->load($_category_id) 



                ?>


                <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>


                <?php endforeach; ?>

But the problem is this displays all the categories the product is assigned to. All i need is the only subcategory's name in which the product is assigned to. Thanks.

There are lot of ways. Quickest and easiest may to use thr Magentk Registry. So try code below:

$category = Mage::registry('current_category')

Or

$categoryIds = Mage::registry('current_product')->getCategoryIds();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM