Question

I need to get category custom image attribute on frontend.

Anyone have solution for this?

Thanks

Was it helpful?

Solution

Please Try my way I get Category custom attribute image name homepage_image

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$catId = 328; /* Category ID */
$categoryCollection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Category\Collection');

$categories = $categoryCollection->addAttributeToSelect('*')->addAttributeToSelect('homepage_category')->addAttributeToFilter('entity_id', array('in' => $catId));
$_helper = $this->helper('Magento\Catalog\Helper\Output'); ?>

<?php foreach ($categories as $category) : ?>
        <?php $homeimg = $category->getHomepageImage();
        if ($homeimg) : ?>
            <div class="box-inner">
                    <div class="homepage-category-img">
                        <div class="category-image-extra">
                            <img src="<?php echo $mediaUrl. 'catalog/category/'$homeimg; ?>" alt="<?php echo $category->getName(); ?>" width="420" />
                            <a href="<?php echo $category->getUrl(); ?>">category</a>
                        </div>
                    </div>
            </div>
        <?php endif; ?>
    <?php endforeach; ?>

OTHER TIPS

You can try using object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$catCustomAttImg = $category->getData('cat_custom_att_img');

if (!empty($catCustomAttImg)) {
    echo '<img src="' . $catCustomAttImg . '" />';
} else {
    // Something else
}

First try to get attribute :-

$category_id = 20;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($category_id);  
print($_category->getData('custom_attribute'));

After this, use media class to show image.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top