app/code/VendoreName/ModuleName/Helper
Data.php
<?php
namespace VendoreName\ModuleName\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
protected $_storeManager;
protected $categoryCollection;
public function __construct(
..............................................................
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
..............................................................
) {
..............................................................
$this->_storeManager = $storeManager;
$this->categoryCollection = $categoryCollection;
..............................................................
}
..............................................................
..............................................................
public function getCurrentStoreId()
{
return $this->_storeManager->getStore()->getId();
}
public function getCategoryList()
{
$collection = $this->categoryCollection->create();
$collection->addAttributeToSelect('*');
$collection->setStore($this->_storeManager->getStore($this->getCurrentStoreId()));
$collection->addAttributeToFilter('is_active', '1');
$collection->addAttributeToFilter('include_in_menu', '1');
$collection->setOrder('name');
return $collection;
}
}
app/code/VendoreName/ModuleName/view/frontend/templates
custom.phtml
<?php
$moduleNameHelper = $this->helper(\VendoreName\ModuleName\Helper\Data::class);
$categories = $moduleNameHelper->getCategoryList();
?>
<?php if ($categories->count()): ?>
<div>
<div >
<ul class="category_section">
<?php foreach ($categories as $categoriesKey => $categoriesVal): ?>
<?php if ($categoriesVal->getLevel() != 1): ?>
<li>
<a href="<?= $block->escapeUrl($categoriesVal->getUrl()) ?>">
<span class="t2 "><?= $block->escapeHtml($categoriesVal->getName()) ?></span>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif; ?>
Add below block to your layout where you want to show
<block class="Magento\Framework\View\Element\Template" name="custom.category.list" template="VendoreName_ModuleName::custom.phtml" />
Add below line into your static block in admin panel
{{block class="Magento\Framework\View\Element\Template" template="VendoreName_ModuleName::custom.phtml"}}