【问题标题】:Get category name from Magento从 Magento 获取类别名称
【发布时间】:2016-09-08 13:38:26
【问题描述】:

我在 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 完全陌生。它没有显示任何错误,但没有显示类别的名称,而它在面包屑标题中可见。我在这里做错了什么?

【问题讨论】:

    标签: php magento magento-1.9


    【解决方案1】:

    如果您想在类别页面上显示类别名称。您可以通过 2 种方式实现这一目标。

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

    或者

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

    【讨论】:

    • 感谢您的帮助
    【解决方案2】:

    在我们的网站 (magento 1.9) 上,我们希望在我们的产品页面上显示当前产品的第一个父类别并提供指向它的链接。我通过以下方式实现了这一点-您应该能够根据自己的目的对我的代码进行逆向工程。 起初我通过将以下代码直接添加到 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>';
    }
    

    【讨论】:

      【解决方案3】:

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

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

      请把分类id放在加载函数中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-25
        相关资源
        最近更新 更多