Magento – how do get all parent of curent category in magento2

categorymagento2menu

ex: I have category list as follows:

Men
–top
—shoes
–bottom

I have current_category is shoes. so how to add class to this and all parent of this.

Best Answer

Using below code you can get parents of current category

<?php 
  /* $block \Magento\Catalog\Block\Category\View */
foreach ($block->getCurrentCategory()->getParentCategories() as $parent) {
    echo $parent->getId();
}
?>
Related Topic