【问题标题】:Show category with images in homepage Magento2在主页 Magento2 中显示带有图像的类别
【发布时间】:2016-02-06 08:57:25
【问题描述】:

在主页 Magento2 中显示带有图像的类别

http://ibnab.com/en/blog/magento-2/magento-2-frontend-how-to-call-category-collection-on-home-page

这篇文章工作正常,但我需要显示类别图像。如何也获取类别图像

我正在使用 $category->getImageUrl();

但它不起作用

【问题讨论】:

标签: magento2


【解决方案1】:

通过结合教程和 R T 的答案,我能够在主页上显示这个。因为我没有在该页面上工作的 _objectManager(并且当我尝试 R T 的代码时页面出现错误),所以我将类别模型包含在块文件(Collection.php)中

protected $_categoryHelper;
protected $categoryFlatConfig;
protected $topMenu;
protected $categoryView;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Helper\Category $categoryHelper,
    \Magento\Catalog\Model\Indexer\Category\Flat\State $categoryFlatState,
    \Magento\Theme\Block\Html\Topmenu $topMenu,
    \Magento\Catalog\Model\Category $categoryView
) {

    $this->_categoryHelper = $categoryHelper;
    $this->categoryFlatConfig = $categoryFlatState;
    $this->topMenu = $topMenu;
    $this->categoryView = $categoryView;
    parent::__construct($context);
}

然后我在块文件底部添加了一个调用 phtml 的方法。

    public function getCategoryView() {
    return $this->categoryView;
}

在 phtml (storecategories.phtml) 中,我将代码更改为像这样工作。

<?php
$categories = $this->getStoreCategories(true,false,true);
$categoryHelper = $this->getCategoryHelper();
?>
<ul>
<?php
    foreach($categories as $category):
        if (!$category->getIsActive()) {
            continue;
        }
?>
    <li><a href="<?php echo $categoryHelper->getCategoryUrl($category) ?>">
<?php
    $catId = $category->getId();
    $categoryAgain = $this->getCategoryView()->load($catId);
    $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
    $_imgHtml   = '';
    if ($_imgUrl = $categoryAgain->getImageUrl()) {
        $_imgHtml = '<img src="' . $_imgUrl . '" />';
        $_imgHtml = $_outputhelper->categoryAttribute($categoryAgain, $_imgHtml, 'image');
        /* @escapeNotVerified */ 
        echo $_imgHtml;
    }
?>
    <?php echo $category->getName() ?></a></li>
    <?php endforeach; ?>
</ul>

然后我将新调用添加到 di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Ibnab\CategoriesSide\Block\CategorisCollection">
    <arguments>
        <argument name="deleteorderAction" xsi:type="array">
            <item name="context" xsi:type="string">\Magento\Framework\View\Element\Template\Context</item>
            <item name="helper" xsi:type="string">\Magento\Catalog\Helper\Category</item>
            <item name="flatstate" xsi:type="string">\Magento\Catalog\Model\Indexer\Category\Flat\State</item>
            <item name="menu" xsi:type="string">\Magento\Theme\Block\Html\Topmenu</item>
            <item name="categoryview" xsi:type="string">\Magento\Catalog\Model\Category</item>
        </argument>
    </arguments>
</type>

【讨论】:

    【解决方案2】:

    我已经在模板中做到了如下:

        $category = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($item->getId());
        $_outputhelper    = $this->helper('Magento\Catalog\Helper\Output');
    
        $_imgHtml   = '';
        if ($_imgUrl = $category->getImageUrl()) {
    
           $_imgHtml = '<img src="' . $_imgUrl . '" />';
        $_imgHtml = $_outputhelper->categoryAttribute($category, $_imgHtml, 'image');
        /* @escapeNotVerified */ 
        echo $_imgHtml;
    }
    

    希望对你有帮助

    【讨论】:

    猜你喜欢
    • 2012-01-30
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多