从 Magento 获取类别名称

Get category name from Magento

我在 Magento 中使用 Luxury 主题。我正在尝试在 catalog/category/view.phtml 文件中显示当前类别名称。

到目前为止我做了什么:

<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
    <div class="container">
        <div class="col-md-12">
            <ul>
                <?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
                    <li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
                    <?php if($_crumbInfo['link']): ?>
                        <a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
                    <?php else: ?>
                        <strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
                    <?php endif; ?>
                    <?php if(!$_crumbInfo['last']): ?>
                        <span>| </span>
                    <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
<?php endif ?>

</div>

我从 page/html/breadcrumbs.phtml 中获取了这段代码。

我对 Magento/PHP 完全陌生。它没有显示任何错误,但它没有显示类别的名称,但它在面包屑中可见 header。我在这里做错了什么?

如果你想在分类页面上显示分类名称。您可以通过两种方式实现。

<?php echo $this->getCurrentCategory()->getName(); ?>

或者

<?php echo Mage::registry('current_category')->getName() ?>

在我们的网站 (magento 1.9) 上,我们希望在我们的产品页面上显示当前产品的第一个父类别,并为其提供 link。我按如下方式实现了这一点——您应该能够根据自己的目的对我的代码进行逆向工程。 起初,我是通过将以下代码直接添加到 catalog/product/view.phtml 来实现的,但后来将其迁移到我自己的模块中的自定义助手中。

这是代码,看看它是否适合你。

//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();

//set CatID to the second element of the array since the first element 
//is the base category of which all others are children.
$_catID = $categoryIds[1];

//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);

//get the level of the current category
$level = $_category->getLevel();

//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){

    // we want the second level category, since the first is the base category.
    // ie if we call the default category 'base' and the product is in category 
    //base->foo->bar->baz we want to return the link to foo.
    // while current category is deeper than 2 we ask it for it's parent  
    //category until we reach the one we want

    while ($level > 2){
            $parent = $_category->getParentId();
            $_category =Mage::getModel('catalog/category')->load($parent);
            $level = $_category->getLevel();
        }

        //Now we can build the string and echo it out to the page
        $caturl = $_category->getUrl_key();
        $_linkstring =  'http://www.yourwebsite.com/' . $caturl . '.html';
        echo 'in category:';
        echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
        echo ' '. $_category->getName();
        echo '</a>';
}

从magento中的类别id获取类别名称、图片、描述

$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();

请将类别id放入加载函数