Magento 2:检查侧边栏中是否存在类别时获取空白页面



我在sidebar.phtml中的当前代码

  <?php
  $objectManager = MagentoFrameworkAppObjectManager::getInstance();
  $category = $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get current category
  if($category->getId()==503){
 echo $block->getLayout()->createBlock('MagentoCmsBlockBlock')->setBlockId('automation_tissue_processors')->toHtml();
}
?>

当我在面包屑中带有类别的页面上时,它工作正常

Home > Instruments > automation-tissue-processors-embedders.html

问题是当面包屑中没有类别时,我得到一个空白页

Home > automation-tissue-processors-embedders.html

当我从搜索结果中点击产品时,就会发生这种情况

有什么方法可以解决这个问题吗?

您正在访问空对象上的 category>getId($category这就是为什么它会导致空白页。只需在编码中使用此代码

if(!empty($category( && $category->getId((==503({ echo $block->getLayout((->createBlock('Magento\Cms\Block\Block'(->setBlockId('automation_tissue_processors'(->toHtml((;}

最新更新