magento 2:检查侧边栏中是否存在类别时出现空白页
magento 2: get blank page when checking if category exists in sidebar
我当前的代码在 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
当我点击搜索结果中的产品时会发生这种情况
有什么办法可以解决这个问题?
您正在访问 null 对象 $category 上的 category>getId(),这就是它导致空白页的原因。只需在 if codition
中使用此代码
if(!empty($category) && $category->getId()==503){
echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('automation_tissue_processors')->toHtml();
}
我当前的代码在 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
当我点击搜索结果中的产品时会发生这种情况
有什么办法可以解决这个问题?
您正在访问 null 对象 $category 上的 category>getId(),这就是它导致空白页的原因。只需在 if codition
中使用此代码if(!empty($category) && $category->getId()==503){ echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('automation_tissue_processors')->toHtml(); }