Need Help for Magento 2, I have cretaed a attribute for category. I want to display featured catagory list on home page using attribute,please help me for get collection of featured category.


First You nedd to create a phtml file in your theme


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryCollection->create();
$categories = $categoryCollection->addAttributeToSelect('*')->addAttributeToSelect('featured_category')->addAttributeToFilter('entity_id', array('in' => $categoryCollection));      
foreach ($categories as $category) {
    echo $category->getId();
    echo $category->getName();
    echo $category->getUrl();

Your attribute is featured_category u define in attribute filter. Please Call this file in your homepage first.

Use of Object manager is not correct method

  • {{block class="Magento\Framework\View\Element\Template" name="featured_category" template="Tagsafari_default::featured_category.phtml"}} IS this currect? – Hardik Radadiya Jun 24 at 10:55
  • {block class="Magento\Framework\View\Element\Template" name="featured_category" template="Tagsafari_Default::featured_category.phtml"}} – Magento_Bhurio Jun 24 at 10:58
  • Error filtering template: Invalid template file: 'Tagsafari_Default::featured_category.phtml' in module: '' block's name: 'magento\framework\view\element\template_0' getting this error – Hardik Radadiya Jun 24 at 11:01
  • Please Try This {block class="Magento\Framework\View\Element\Template" name="featured_category" template="Tagsafari_Default::Tagsafari_Default::featured-category.phtml"}} – Magento_Bhurio Jun 24 at 11:03
  • this time also getting same error – Hardik Radadiya Jun 24 at 11:05

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.