I previously used ObjectManager
to get current category image. but it sometimes caused problems to me. Like loading blank page without no content, slower loading time etc.. , also I saw in a few questions that people commented that using ObjectManager
directly is not a good approach.
This is the page source when I get blank page error.
<img class="img-responsive" src="<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function getImageUrl() on null in E:\xampp\htdocs\magento_projects\abc\app\design\frontend\abcvendor\abctheme\Magento_Catalog\templates\product\view\brandlogoimage.phtml:37
Stack trace:
#0 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\TemplateEngine\Php.php(59): include()
#1 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Template.php(255): Magento\Framework\View\TemplateEngine\Php->render(Object(Magento\Catalog\Block\Product\View\Interceptor), 'E:/xampp/htdocs...', Array)
#2 E:\xampp\htdocs\magento_projects\abc\var\generation\Magento\Catalog\Block\Product\View\Interceptor.php(544): Magento\Framework\View\Element\Template->fetchView('E:/xampp/htdocs...')
#3 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Template.php(279): Magento\Catalog\Block\Product\View\Interceptor->fetchView('E:/xampp/htdocs...')
#4 E:\xampp\htdocs\magento_projects\abc\vendor\magento\framework\View\Element\Abstrac in <b>E:\xampp\htdocs\magento_projects\abc\app\design\frontend\abcvendor\abctheme\Magento_Catalog\templates\product\view\brandlogoimage.phtml</b> on line <b>37</b><br />
This is the code I used to get category in my page, I used phtml file.
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); ?>
<img class="img-responsive" src="<?php echo $category->getImageUrl(); ?>" alt="brandlogo" /></a>
How can I prevent that blank page loading error?
Is there any better approach to get category image than method I've used here?
Best Answer
try this code its work for me
add this code in vendor/magento/module-catalog/view/frontend/templates/product/view/details.phtml file and check
Please override product detail page.