0

How I can add a class "active" to the current / selected category? Magento 2.3

<?php
 // get the list of all categories
//echo "Hello";
//echo '<div class="hello"></div>';
$_categories = $block->getCategoryCollection(); 

//var_dump($categories); //echo "end";

 $category = $block->getCurrentCategory();
 $categoryId=$category->getId();

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $subCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
 $subCats = $subCategory->getChildrenCategories();
 $_helper = $this->helper('Magento\Catalog\Helper\Output');

 ?>

 <ul class="category-main">
 <?php foreach($_categories as $_category): ?>
    <li class="category">
        <a href="<?php echo $_category->getUrl(); ?>">
            <?php echo $_category->getName(); ?>
        </a>
        <?php if ($category->getId() && $category->getId() == $_category->getId()): ?>
            <?php $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($_category->getId());
            ?>
            <?php $subCats = $subCategory->getChildrenCategories(); ?>
                <ul class="subcategory-main">
                    <?php foreach($subCats as $subcat): ?>
                        <li class="subcategory">
                                <a class="" href="<?php echo $subcat->getUrl(); ?>">
                                    <?php echo $subcat->getName(); ?>
                                </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

How I can add a class "active" to the current / selected category?

Thank you

1

Try this below code :

$category = $block->getCurrentCategory();
$categoryId=$category->getId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$subCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');

?>

 <ul class="category-main">
 <?php foreach($_categories as $_category): ?>
    <li class="category">
        <a href="<?php echo $_category->getUrl(); ?>">
            <?php echo $_category->getName(); ?>
        </a>
        <?php if ($category->getId() && $category->getId() == $_category->getId()): ?>
            <?php $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($_category->getId());
            ?>
            <?php $subCats = $subCategory->getChildrenCategories(); ?>
                <ul class="subcategory-main">
                    <?php foreach($subCats as $subcat): ?>
                        <?php $class = ($categoryId == $subcat->getId())?'active':''; ?>
                        <li class="subcategory">
                                <a class="<?php echo $class; ?>" href="<?php echo $subcat->getUrl(); ?>">
                                    <?php echo $subcat->getName(); ?>
                                </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

I added this below line in foreach :

<?php $class = ($categoryId == $subcat->getId())?'active':''; ?>

Make sure $categoryId is your current category id.

Hope it will helpful for you.

| improve this answer | |
  • yes $categoryId is my current category id but which area put this code @Rohan Hapani – Ravindrasinh Zala Jul 30 at 6:06
  • i try but is not working @Rohan Hapani – Ravindrasinh Zala Jul 30 at 6:18
0

Try this below code:

app/code/vendorname/modulename/view/frontend/templates/categories.php

<?php
// get the list of all categories
//echo "Hello";
//echo '<div class="hello"></div>';
$_categories = $block->getCategoryCollection(); 
//var_dump($categories);
//echo "end";

$category = $block->getCurrentCategory();
$categoryId=$category->getId();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$subCategory = $objectManager->create('Magento\Catalog\Model\Category')- 
>load($categoryId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');

?>

<ul class="category-main">
<?php foreach($_categories as $_category): ?>
    <?php $class = ($categoryId == $_category->getId())?'active':''; ?>
    <li class="category">
        <a class="<?php echo $class; ?>" href="<?php echo $_category->getUrl(); ?>">
            <?php echo $_category->getName(); ?>
        </a>
        <?php if ($category->getId() && $category->getId() == $_category->getId()): ?>
            <?php $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($_category->getId());
            ?>
            <?php $subCats = $subCategory->getChildrenCategories(); ?>
                <ul class="subcategory-main">
                    <?php foreach($subCats as $subcat): ?>
                        <li class="subcategory">
                                <a class="" href="<?php echo $subcat->getUrl(); ?>">
                                    <?php echo $subcat->getName(); ?>
                                </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
        <?php endif; ?>
    </li>
<?php endforeach; ?>

Css

li.category a.active
{
   color: white !important;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.