2017-03-29 50 views
0
存在类别时获得空白页

我目前在sidebar.phtml代码Magento的2:检查是否在侧边栏

<?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 

这种情况发生在我点击产品来自搜索结果

有什么办法解决这个问题?

回答

0

您正在访问空对象$ category上的类别> getId(),这就是为什么它会导致空白页面。 > createBlock('Magento的\ CMS - 只是如果codition

如果(!空($类)& & $分类 - >的getId()== 503){ 回声$嵌段 - > getLayout()使用此代码\块\块 ') - > setBlockId(' automation_tissue_processors') - > toHtml(); }