【问题标题】:Get Category Description in my cutom phtml file in magento 2在 magento 2 的自定义 phtml 文件中获取类别描述
【发布时间】:2020-03-11 09:00:17
【问题描述】:

我正在使用 Magento 2。我想在我的自定义 phtml 文件中获取类别描述和图像。 我该怎么做?

【问题讨论】:

    标签: categories magento2 descriptor


    【解决方案1】:

    如果您想在 phtml 中获取当前类别信息,可以使用以下代码。

    <?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $currentCategory = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category information
    echo $currentCategory->getId();
    echo $currentCategory->getName();
    echo $currentCategory->getDescription();
    echo $currentCategory->getImageUrl();
    ?>
    

    或者你想加载任何特定的类别然后你可以使用下面的代码。

    $categoryId = 3;
    $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $_objectManager->create('Magento\Catalog\Model\Category')
    ->load($categoryId);
    echo $category->getName();
    echo $category->getImageUrl();
    

    【讨论】:

    • 如何获取分类图片?
    • 这将获取 url。这会打印图像吗?
    • 是的,这将为您提供可以在图像标签中使用该 url 的 url。如果你想输出 magento 默认图像,那么你必须使用 $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
    • 如果以上解决方案解决了您的问题,请点赞。
    • 谢谢,这行得通。你能告诉我如何避免从类别页面的默认位置打印描述和图像吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多