doujiao9866 2015-09-15 16:06
浏览 28
已采纳

页面失败,调用非对象错误的成员函数getMetaTitle()

On magento, my product pages are showing a white screen. The error printed is Fatal error: Call to a member function getMetaTitle() on a non-object in /app/code/core/Mage/Catalog/Block/Category/View.php on line 44

Here are lines 42-46:

    if ($headBlock = $this->getLayout()->getBlock('head')) {
        $category = $this->getCurrentCategory();
        if ($title = $category->getMetaTitle()) {
            $headBlock->setTitle($title);
        }

Not sure how to fix this as the code looks fine to me.

Updated code:

  if ($headBlock = $this->getLayout()->getBlock('head')) {
        $cat_id = $this->getCurrentCategory()->getId();
        $category = Mage::getModel('catalog/category')->load($cat_id);            
        if ($title = $category->getMetaTitle()) {
            $headBlock->setTitle($title);
        }
        if ($description = $category->getMetaDescription()) {
            $headBlock->setDescription($description);
        }
        if ($keywords = $category->getMetaKeywords()) {
            $headBlock->setKeywords($keywords);
        }
        if ($this->helper('catalog/category')->canUseCanonicalTag()) {
            $headBlock->addLinkRel('canonical', $category->getUrl());
        }
  • 写回答

3条回答 默认 最新

  • dt56449492 2015-10-01 14:25
    关注

    Perhaps you modified the original code which is causing the error. Revert back to original code.

        <?php
    /**
    * Magento
    *
    * NOTICE OF LICENSE
    *
    * This source file is subject to the Open Software License (OSL 3.0)
    * that is bundled with this package in the file LICENSE.txt.
    * It is also available through the world-wide-web at this URL:
    * http://opensource.org/licenses/osl-3.0.php
    * If you did not receive a copy of the license and are unable to
    * obtain it through the world-wide-web, please send an email
    * to license@magentocommerce.com so we can send you a copy immediately.
    *
    * DISCLAIMER
    *
    * Do not edit or add to this file if you wish to upgrade Magento to newer
    * versions in the future. If you wish to customize Magento for your
    * needs please refer to http://www.magentocommerce.com for more information.
    *
    * @category   Mage
    * @package    Mage_Catalog
    * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
    * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
    */
    
    /**
    * Category View block
    *
    * @category   Mage
    * @package    Mage_Catalog
    * @author      Magento Core Team <core@magentocommerce.com>
    */
    class Mage_Catalog_Block_Category_View extends Mage_Core_Block_Template
    {
    protected function _prepareLayout()
    {
             parent::_prepareLayout();
    
             $this->getLayout()->createBlock('catalog/breadcrumbs');
    
             if ($headBlock = $this->getLayout()->getBlock('head')) {
                 if ($title = $this->getCurrentCategory()->getMetaTitle()) {
                     $headBlock->setTitle($title);
                 }
                 if ($description = $this->getCurrentCategory()->getMetaDescription()) {
                     $headBlock->setDescription($description);
                 }
                 if ($keywords = $this->getCurrentCategory()->getMetaKeywords()) {
                     $headBlock->setKeywords($keywords);
                 }
                 /*
                 want to show rss feed in the url
                 */
                 if ($this->IsRssCatalogEnable() && $this->IsTopCategory()) {
                     $title = $this->helper('rss')->__('%s RSS Feed',$this->getCurrentCategory()->getName());
                     $headBlock->addItem('rss', $this->getRssLink(), 'title="'.$title.'"');
                 }
             }
    
             return $this;
         }
    
         public function IsRssCatalogEnable()
         {
             return Mage::getStoreConfig('rss/catalog/category');
         }
    
         public function IsTopCategory()
         {
             return $this->getCurrentCategory()->getLevel()==2;
         }
    
         public function getRssLink()
         {
             return Mage::getUrl('rss/catalog/category',array('cid' => $this->getCurrentCategory()->getId(), 'store_id' => Mage::app()->getStore()->getId()));
         }
    
         public function getProductListHtml()
         {
             return $this->getChildHtml('product_list');
         }
    
         /**
          * Retrieve current category model object
          *
          * @return Mage_Catalog_Model_Category
          */
         public function getCurrentCategory()
         {
             if (!$this->hasData('current_category')) {
                 $this->setData('current_category', Mage::registry('current_category'));
             }
             return $this->getData('current_category');
         }
    
         public function getCmsBlockHtml()
         {
             if (!$this->getData('cms_block_html')) {
                 $html = $this->getLayout()->createBlock('cms/block')
                     ->setBlockId($this->getCurrentCategory()->getLandingPage())
                     ->toHtml();
                 $this->setData('cms_block_html', $html);
             }
             return $this->getData('cms_block_html');
         }
    
         public function isProductMode()
         {
             return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PRODUCT;
         }
    
         public function isMixedMode()
         {
             return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_MIXED;
         }
    
         public function isContentMode()
         {
             return $this->getCurrentCategory()->getDisplayMode()==Mage_Catalog_Model_Category::DM_PAGE;
         }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效