3

I need to get category custom image attribute on frontend.

Anyone have solution for this?

Thanks

Abdul Pathan
2,84212 silver badges22 bronze badges
asked Jun 21, 2019 at 10:31
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; ?>
answered Jun 21, 2019 at 10:58
5
  • @Bhurio I am getting only image name. How to get catalog base url? 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(); ?>"> Commented Jun 21, 2019 at 12:59
  • Please update your comment 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
}
answered Jun 21, 2019 at 10:49
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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.