0

I followed some instructions to do "Add new Image attribute for category in magento 2" and I was successful.and now I want to output all newly added images printed out in the same place on all pages can someone help me to solve this problem. Thank for all

2 Answers 2

1

You can use the Category Model for this. For example, your category Id is 10 and your custom attribute is category_new_image. Check the below code as per this configuration:

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryId = 10;
$category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$category_new_image = $category->getCategoryNewImage(); // your custom attribute value
echo $category_new_image;
?>
1

Try this code

protected $_categoryFactory;

protected $_storeManager;

protected $_categoryNameFactory;

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\CategoryFactory $categoryNameFactory,
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    array $data = []
)
{
    $this->_categoryNameFactory = $categoryNameFactory;
    $this->_categoryFactory = $collecionFactory;
    $this->_storeManager = $storeManager;
    parent::__construct($context, $data);
}


public function getCategoryWithImage()
{
    $category = $this->_categoryFactory->create()->addAttributeToFilter('Your_Attribute_code',['neq' => 'NULL'])->setStore($this->_storeManager->getStore());
    return $category;
}

public function getCategoryName($categoryId)
{
    $category = $this->_categoryNameFactory->create()->load($categoryId)->setStore($this->_storeManager->getStore());
    return $category;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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