Create Custom module with your PackageName and ModuleName
1.Create a registration.php file at
PackageName/ModuleName/registration.php
Add below code
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PackageName_ModuleName',
__DIR__
);
2.Create a module.xml file at
PackageName/ModuleName/etc/module.xml
Add below code
<config xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="PackageName_ModuleName" setup_version="1.0.0"/>
</config>
3.Create a CategoriesList.php file at
PackageName\moduleName\Block\CategoriesList.php
Add below code
<?php
namespace PackageName\moduleName\Block;
class CategoriesList extends \Magento\Framework\View\Element\Template
{
protected $_categoryCollectionFactory;
protected $_categoryHelper;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Helper\Category $categoryHelper,
array $data = []
)
{
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_categoryHelper = $categoryHelper;
parent::__construct($context, $data);
}
public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false)
{
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
if ($isActive) {
$collection->addIsActiveFilter();
}
if ($level) {
$collection->addLevelFilter($level);
}
if ($sortBy) {
$collection->addOrderField($sortBy);
}
if ($pageSize) {
$collection->setPageSize($pageSize);
}
return $collection;
}
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted = false, $asCollection = false, $toLoad = true);
}
}
?>
4.Create topmenu.phtml tempate file at
PackageName/ModuleName/view/frontend/templates/html/topmenu.phtml
Add below code
<ul class="your-class">
<?php /* @escapeNotVerified */ echo $_menu; ?>
$categories = $block->getCategoryCollection();
foreach ($categories as $category) {
//print_r($category->getData());
echo $category->getName() . '<br />';
}
// get categories sorted by category 'name'
$categories = $block->getCategoryCollection(true, false, 'name', false);
foreach ($categories as $category) {
echo $category->getName() . '<br />';
}
// get current store's categories
$categories = $block->getStoreCategories();
foreach ($categories as $category) {
echo $category->getName() . '<br />';
}
</ul>
5.Create default.xml layout file at (render your template at top menu postion )
PackageName/ModuleName/view/frontend/layout/default.xml
Add below code
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name='catalog.topnav'>
<action method='setTemplate'>
<argument name='template' xsi:type='string'>PackageName_ModuleName::html/topmenu.phtml</argument>
</action>
</referenceBlock>
</body>
</page>