3

I need to get category custom image attribute on frontend.

Anyone have solution for this?

Thanks

2
  • have you try any code and do you face any error? Commented Jun 21, 2019 at 10:33
  • U See my Updated Answer or Comment.......? Commented Jun 21, 2019 at 12:47

3 Answers 3

2

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; ?>
5
  • @Bhurio I am getting only image name. How to get catalog base url?
    – Shomita
    Commented Jun 21, 2019 at 12:41
  • See my code i added href for category url.. Commented Jun 21, 2019 at 12:44
  • I get url like this--------$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); <img class="img-fluid mx-auto" src="<?php echo $mediaUrl.'catalog/category/'.$homeimg; ?>" alt="<?php echo $category->getName(); ?>">
    – Shomita
    Commented Jun 21, 2019 at 12:59
  • Please update your comment
    – Shomita
    Commented Jun 21, 2019 at 13:00
  • I update my code please check....... Commented Jun 21, 2019 at 13:04
0

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
}
0

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.

3
  • have you tried??
    – temper
    Commented Jun 21, 2019 at 11:34
  • It's not working.
    – Shomita
    Commented Jun 21, 2019 at 11:39
  • I had updated my answer please try once.
    – temper
    Commented Jun 21, 2019 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.