简体   繁体   English

Magento在产品查看页面上显示所有类别以及父类别

[英]Magento display all categories on product view page with parent categories

Following on from this question: Display ALL categories that a product belongs to in Magento 接下来是这个问题: 在Magento中显示产品所属的所有类别

Is there a way to display the full category path (with links at each stage) rather than only displaying the final category that a product belongs to? 有没有办法显示完整的类别路径(在每个阶段都有链接),而不是仅显示产品所属的最终类别?

I have this code so far... 到目前为止,我已经有了这段代码...

<?php
            $currentCatIds = $_product->getCategoryIds();
            $categoryCollection = Mage::getResourceModel('catalog/category_collection')
                 ->addAttributeToSelect('name')
                 ->addAttributeToSelect('url')
                 ->addAttributeToFilter('entity_id', $currentCatIds)
                 ->addIsActiveFilter();
            foreach($categoryCollection as $cat){
            ?>
                <a href="<?php echo $cat->getUrl(); ?>">
                    <?php echo $cat->getName() ?>
                </a>
            <?php } ?>

Which correctly links the category name that is displayed on the page. 哪个可以正确链接页面上显示的类别名称。 What I would like is to display the full Cat > Sub Cat > Sub Sub Cat trail, and have each element in that trail correctly linked. 我想要显示完整的Cat> Sub Cat> Sub Sub Cat跟踪,并正确链接该跟踪中的每个元素。

How about this: 这个怎么样:

foreach($categoryCollection as $cat){
    $parents = $cat->getCollection()
        ->addIdFilter($cat->getParentIds())
        ->addAttributeToSelect('name')
        ->addUrlRewriteToResult()
        ->setOrder('level');
    foreach ($parents as $parentCat) {
        // Build your parent links
    }
}

By the way, this kind of code doesn't belong in the template. 顺便说一句,这种代码不属于模板。 It should go into a method of the block being rendered (or at least into a helper). 它应该进入正在渲染的块的方法中(或至少进入助手中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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