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
 
 
 
 Shomita 
 
 1,1251 gold badge12 silver badges40 bronze badges
 
 - 
 have you try any code and do you face any error?Dhiren Vasoya– Dhiren Vasoya2019年06月21日 10:33:09 +00:00Commented Jun 21, 2019 at 10:33
- 
 U See my Updated Answer or Comment.......?Magento_Bhurio– Magento_Bhurio2019年06月21日 12:47:51 +00:00Commented Jun 21, 2019 at 12:47
3 Answers 3
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
 
 
 
 Magento_Bhurio 
 
 9385 silver badges15 bronze badges
 
 - 
 @Bhurio I am getting only image name. How to get catalog base url?Shomita– Shomita2019年06月21日 12:41:15 +00:00Commented Jun 21, 2019 at 12:41
- 
 See my code i added href for category url..Magento_Bhurio– Magento_Bhurio2019年06月21日 12:44:03 +00:00Commented 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– Shomita2019年06月21日 12:59:22 +00:00Commented Jun 21, 2019 at 12:59
- 
 Please update your commentShomita– Shomita2019年06月21日 13:00:16 +00:00Commented Jun 21, 2019 at 13:00
- 
 I update my code please check.......Magento_Bhurio– Magento_Bhurio2019年06月21日 13:04:31 +00:00Commented Jun 21, 2019 at 13:04
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
 
 
 
 Faisal Sheikh 
 
 1,3901 gold badge9 silver badges18 bronze badges
 
 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.
- 
 
- 
 
- 
 I had updated my answer please try once.temper– temper2019年06月21日 11:42:41 +00:00Commented Jun 21, 2019 at 11:42
Explore related questions
See similar questions with these tags.
default