我当前在 sidebar.phtml 中的代码
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
if($category->getId()==503){
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('automation_tissue_processors')->toHtml();
}
?>
当我在面包屑中包含类别的页面上时,它工作正常
Home > Instruments > automation-tissue-processors-embedders.html
问题是当面包屑中没有类别时,我会得到一个空白页面
Home > automation-tissue-processors-embedders.html
当我点击搜索结果中的产品时会发生这种情况
有任何解决这个问题的方法吗?
您正在访问空对象 $category 上的 category>getId() 这就是它导致空白页面的原因。只需在 if codition 中使用此代码
if(!empty($category) && $category->getId()==503){ echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('automation_tissue_processors' )->toHtml(); }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句