0

How to display all categories under the static text in frontend Menu in Magento2

Example: We have 3 categories created in admin and displaying like below in frontend

Menu 1 Menu 2 Menu 3

We need to display like below

CATEGORIES

  • Menu 1
  • Menu 2
  • Menu 3
asked Sep 21, 2021 at 13:46

2 Answers 2

0

You can create your own custom version of Magento_Theme/templates/html/topmenu.phtml in your own theme under app/design/frontend/<Vendor_Name>/<Theme_Name>/Magento_Theme/templates/html/topmenu.phtml. From there you can add a <div class='navigation'>Categories</div> above the <ul> that contains the contents of $_menuHtml.

Note: If you are extending the Luma theme you will need to add some css to get rid of the display: none on nav-sections-item-content > * which has display: none.

answered Oct 19, 2021 at 12:59
0

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"}}
answered Oct 27, 2021 at 3:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.