php Magento:显示子类别列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1820012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:57:27  来源:igfitidea点击:

Magento: Display sub-category list

phpmagentomagento-1.9

提问by doubleplusgood

I'm building a Magento store and want to be able to display a list of categories and have each category link to its own page.

我正在建立一个 Magento 商店并希望能够显示类别列表并将每个类别链接到自己的页面。

I have a 'Brands' category with an ID of 42 and I want to display a list of the sub-categories and ensure that each one links to the designated URL key in the CMS.

我有一个 ID 为 42 的“品牌”类别,我想显示子类别列表,并确保每个类别都链接到 CMS 中指定的 URL 键。

Has anyone had experience of doing this with Magento?

有没有人有过用 Magento 做这件事的经验?

回答by wookiehangover

If you're comfortable editing your theme, this code snippet will bring you a list of all sub-categories of the current category (from the session, so this should work anywhere in your theme). I typically use this in app/design/frontend/default/theme_name/template/catalog/category/view.phtml

如果您愿意编辑主题,此代码段将为您提供当前类别的所有子类别的列表(来自会话,因此这应该适用于您主题的任何地方)。我通常在 app/design/frontend/default/ theme_name/template/catalog/category/view.phtml 中使用它

<?php
$_category  = $this->getCurrentCategory(); 
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper     = Mage::helper('catalog/category');
?>

<ul>
    <?php foreach ($collection as $cat):?>
            <?php if($_category->getIsActive()):?>
                <?php 
                     $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
                     $_img = $cur_category->getImageUrl();  
                ?>
                <li>
                    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
                         <img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
                         <cite><?php echo $cat->getName();?></cite>
                    </a>
                </li>
            <?php endif?>
    <?php endforeach;?>
</ul>

回答by ScoRpion

If You want to Display top level categories and subcategories U can do Like This..

如果您想显示顶级类别和子类别,您可以这样做。

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
    <?php foreach($_categories as $_category): ?>
        <li>
            <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                <?php echo $_category->getName() ?>
            </a>
            <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php if (count($_subcategories) > 0): ?>
                <ul>
                    <?php foreach($_subcategories as $_subcategory): ?>
                        <li>
                            <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                <?php echo $_subcategory->getName() ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

For Displaying Top Level Categories and Current Categories SubCategories you can Do like ....

要显示顶级类别和当前类别子类别,您可以这样做....

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
    <?php foreach($_categories as $_category): ?>
        <li>
            <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                <?php echo $_category->getName() ?>
            </a>
            <?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

回答by Kathir Sid Vel

This question requires a long answer. I will point you to the right places.

这个问题需要很长的答案。我会告诉你正确的地方。

1) Best solution is to use the free extension. I haven't tried it, but it will suit the purpose. You will have to do some CSS to achieve the right look and feel.

1) 最佳解决方案是使用免费扩展。我没试过,但它会适合这个目的。你将不得不做一些 CSS 来实现正确的外观和感觉。

http://www.magentocommerce.com/extension/1562/magento-easy-catalog-imagesDemo: http://extension01.templates-master.com/gb/electronics.html

http://www.magentocommerce.com/extension/1562/magento-easy-catalog-images演示:http: //extension01.templates-master.com/gb/electronics.html

2) I do not trust in modules as it might become difficult to upgrade if the vendor decided to stop supporting it. I have used the information from the following forum thread to create a vew sites. Have a look... Might not be straight forward. You might have to make some copies of core files to the local directory.

2) 我不相信模块,因为如果供应商决定停止支持它,升级可能会变得困难。我已经使用以下论坛帖子中的信息创建了一个 vew 站点。看一看...可能不是直截了当的。您可能需要将核心文件的一些副本复制到本地目录。

http://www.magentocommerce.com/boards/viewthread/3770/P30/

http://www.magentocommerce.com/boards/viewthread/3770/P30/

Hopefully this will be of help to you :)

希望这对你有帮助:)

回答by Devin R. Olsen

I made this little video on how I create custom category listing blocks with Magento. I am sure there are better ways of achieving this or even something I could have done better, but it's just my method. I only created this it in hopes that it helps explain somethings to some people out there.

我制作了这个关于如何使用 Magento 创建自定义类别列表块的小视频。我相信有更好的方法来实现这一点,甚至还有一些我可以做得更好的方法,但这只是我的方法。我创建它只是希望它有助于向那里的某些人解释一些事情。

Magento Custom Category Listing Block

Magento 自定义类别列表块

Thanks!

谢谢!

回答by Jason

after looking at all the solutions on the magento site, i found that wookiehangover's solution above worked and took about 8 seconds to implement.

在查看了 magento 站点上的所有解决方案后,我发现上述 wookiehangover 的解决方案有效,并且实施了大约 8 秒。

creates a UL that you can style. thanks.

创建一个您可以设计的 UL。谢谢。

回答by Orkun Alabaz

After creating static block you can get any list of the subcategories by this script:

创建静态块后,您可以通过此脚本获取任何子类别列表:

        $_helper = Mage::helper('catalog/category');
        $_category = Mage::getModel('catalog/category')->load(5);
        $_subcategories = $_category->getChildrenCategories();

        if (count($_subcategories) <= 0) { return; }

        $count = 0;

        foreach($_subcategories as $_category) {     
                                                      $category = Mage::getModel('catalog/category')->load($_category->getId());

                                                      $ret->{"object_".$count} ->url  = $_helper->getCategoryUrl($_category);
                                                      $ret->{"object_".$count} ->name = $_category->getName();
                                                      $ret->{"object_".$count} ->id =  $_category->getId(); 
                                                      $ret->{"object_".$count} ->image =   $category->getImageUrl();
                                                      $count++;
                                                   } 

        return $ret;                                          

        } 


$list = list_subcategories(5);

echo "<pre>"; print_r($list); echo "</pre>";
?>

回答by cveto

How about listing only the categories belonging to the current item. Not all the categories on the page.

如何仅列出属于当前项目的类别。并非页面上的所有类别。

But in a tree like view.

但在树状视图中。

CATEGORIE - sub cat 1 CATEGORIE 2 - sub cat 1 - sub sub cat 1

CATEGORIE - 子猫 1 CATEGORIE 2 - 子猫 1 - 子子猫 1

BR Cveto

BR Cveto