我正在为 Magento 2 构建一个主题。主页需要遍历所有类别并输出类别名称、url 和上传到 .phtml 文件中管理员内类别的图像。
这是可能的,我怎样才能做到这一点?
提前致谢 :)
小智 5
我希望下面的代码可以帮助你。
我们使用了直接对象管理器。请在您的 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 }?>
Run Code Online (Sandbox Code Playgroud)
也请检查此链接-http://blog.chapagain.com.np/magento-2-get-parent-category-children-categories-product-count/