You can create a helper class and use your help to get category icon Url as described below.
I assume your custom extension name is "Vendor_Module". I also assume that your custom category attribute name is "caticon"
step 1) Create a helper class Data.php under /app/code/Vendor/Module/Helper/
File : Data.php
<?php
namespace Vendor\Module\Helper;
class Data
extends \Magento\Framework\App\Helper\AbstractHelper
{
// category attribute name
const ATTRIBUTE_NAME = "caticon";
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->_storeManager = $storeManager;
}
/**
* Retrieve Icon image URL by category
*
* @return string
*/
public function getImageUrl(\Magento\Catalog\Model\Category $category)
{
$image = $category->getData(self::ATTRIBUTE_NAME);
return $this->getUrl($image);
}
/**
* Retrieve URL
*
* @return string
*/
public function getUrl($value)
{
$url = false;
if ($value) {
if (is_string($value)) {
$url = $this->_storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'pub/media/' . $value;
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('Something went wrong while getting the image url.')
);
}
}
return $url;
}
}
step 2) Run Di compile commands from CLI
php bin/magento setup:di:compile
step 3) How to use your helper to get category icon url in a phtml file.
Sample example phmtl file:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
$_helper = $this->helper('Vendor\Module\Helper\Data');
$categoryId = 10;
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$categoryIconUrl = $_helper->getUrl($category->getCaticon()); // i.e get icon Attribute
// or you can use below line of code to get icon image by category object
// $categoryIconUrl = $_helper->getImageUrl($category)
?>
<img src="<?php echo $categoryIconUrl?>" />