cancel
Showing results for 
Search instead for 
Did you mean: 

Display a categories page with image in Magento2

Display a categories page with image in Magento2

I'm beginner on Magento and i'm working with Magento2. I already created my categories.phtm and show it in a CMS page but i want to show sub-categories of my root category in a this page like a grid with images and title.

My categories structure is like this :

+ Products( this is my root caterogy)

+ cat 1

+ cat 2

+ cat 3

+ cat 4

Thank you

2 REPLIES 2

Re: Display a categories page with image in Magento2

You can check this link:

http://stackoverflow.com/questions/35239192/show-category-with-images-in-homepage-magento2/35725008#...

 

OpenSource Expert | OpenSource Technologies | www.opensourcetechnologies.com
Magento plugins released: http://www.opensourcetechnologies.com/product/product-category/magento-extensions

Re: Display a categories page with image in Magento2

Hello Farisshindsite,

 

Create a block and add following code

  

<?php
namespace Ipragmatech\Bannerblock\Block;

use Magento\Catalog\Helper\Category;

class Popularmenu extends \Magento\Framework\View\Element\Template
{

    protected $_categoryHelper;

    protected $_categoryFlatConfig;
    protected $_categoryFactory

    public function __construct(
        \Magento\Catalog\Helper\Category $catalogCategory,
        \Magento\Catalog\Model\Indexer\Category\Flat\State  $categoryFlatState, 
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
    ) {
        $this->_categoryHelper = $catalogCategory;
        $this->_categoryFlatConfig = $categoryFlatState;
        $this->_categoryFactory = $categoryFactory;
    }

    /*
     * Return categories helper
     */
    public function getStoreCategories(
        $sorted = false,
        $asCollection = false,
        $toLoad = true
    ) {
        return $this->_categoryHelper->getStoreCategories($sorted,
            $asCollection, $toLoad);
    }
    
    // sub category

    public function getChildCategories($category)
    {
        if ($this->_categoryFlatConfig->isFlatEnabled() &&
            $category->getUseFlatResource()) {
            $subcategories = (array)$category->getChildrenNodes();
        } else {
            $subcategories = $category->getChildren();
        }

        return $subcategories;
    }

  public function getCategorymodel($id)
    {
        $_category = $this->_categoryFactory->create();
        $_category->load($id);
        return $_category;
    }
}

 on phtml file it can be accessed as 

  

<?php $isEnable = $this->helper('Ipragmatech\Bannerblock\Helper\Data')->getConfig('bannerblocksection/general/banneractive'); ?>
<?php
// $productCollection = $block->getProductCollection();
$categoryIds = $block->getCategoryList();
<?php
$categories = $block->getStoreCategories();
?>
<div class="popular-menu">
	<div class="content-heading">
		<h2 class="title"><?php echo $this->getTitle(); ?></h2>
	</div>
	<ul class="popular-menu-ul">
		<?php if(count($categories)):?>
			<?php foreach ($categories as $catrgory):?>
			<?php $category = $this->getCategorymodel($catrgoryId->getEntityId())?>
				<li class="popular-menu-li"><span class="pop-menu"> <a
				href="<?php echo $category->getUrl(); ?>"> <span class="cat-title"><?php echo $category->getName(); ?></span>
			</a>
		</span></li>
			<?php endforeach;?>
		<?php endif;?>
	</ul>
</div>