Skip to main content
Tweeted twitter.com/StackMagento/status/1403049321457766401
deleted 47 characters in body
Source Link

Can anyone point me in the right direction?

Can anyone point me in the right direction?

Source Link
Jimmery
  • 1.5k
  • 5
  • 24
  • 38

Getting Sub Category Images in Magento2

I am trying to get at Category Images from within Magento_Catalog\template\navigation\left.phtml. This file shows a list of sub-categories.

This is that file:

<?php if (!$block->getCategory()) {
    return;
} ?>
<?php $_categories = $block->getCurrentChildCategories(); ?>
<?php $_count = is_array($_categories) ? count($_categories) : $_categories->count(); ?>
<?php if ($_count): ?>
    <div class="block filter">
        <div class="title">
            <strong><?php /* @escapeNotVerified */ echo __('Shop By') ?></strong>
        </div>
        <div class="content">
            <strong class="subtitle"><?php /* @escapeNotVerified */ echo __('Shopping Options') ?></strong>
            <dl class="options" id="narrow-by-list2">
                <dt><?php /* @escapeNotVerified */ echo __('Category') ?></dt>
                <dd>
                    <ol class="items">
                        <?php foreach ($_categories as $_category): ?>
                            <?php if ($_category->getIsActive()): ?>
                                <li class="item">
                                    <a href="<?php /* @escapeNotVerified */ echo $block->getCategoryUrl($_category) ?>"<?php if ($block->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
                                        <img src="<?php echo $block->getCategory($_category)->getImageUrl(); ?>">
                                        <p><?php echo $block->escapeHtml($_category->getName()) ?></p>
                                    </a>
                                    <span class="count"><?php /* @escapeNotVerified */ echo $_category->getProductCount() ?></span>
                                </li>
                            <?php endif; ?>
                        <?php endforeach ?>
                    </ol>
                </dd>
            </dl>
        </div>
    </div>
<?php endif; ?>

The line I was hoping would work is:

$block->getCategory($_category)->getImageUrl();

However this is returning the image for the current category, not the sub-categories.

Can anyone point me in the right direction?