gpt4 book ai didi

magento2 - Magento 2 - 如何循环所有类别并加载类别图像?

转载 作者:行者123 更新时间:2023-12-02 20:41:10 25 4
gpt4 key购买 nike

我正在为 Magento 2 构建一个主题。主页需要循环遍历所有类别,并在 .phtml 文件中输出类别名称、URL 和上传到管理内类别的图像。

这可能吗?我怎样才能实现这一目标?

提前致谢:)

最佳答案

希望以下代码对您有所帮助。

我们使用了直接对象管理器。请在您的 phtml 文件中使用此代码并在任何地方运行。

<?php
//create a phtml file or write this code in any phtml file and use where you want

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categories = $categoryHelper->getStoreCategories();

foreach ($categories as $category) {
//print_r($category->getData()); ?>
<a href="<?php echo $category->getUrl();?>"><?php echo $category->getName() ;?></a>

<?php }?>

另请检查此链接 -http://blog.chapagain.com.np/magento-2-get-parent-category-children-categories-product-count/

关于magento2 - Magento 2 - 如何循环所有类别并加载类别图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com