如何从类别模板(view.phtml)之外获取当前类别图像? 例如,如果我想从header.phtml或footer.phtml获取它。
我尝试从view.phtml复制代码:
<?php
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
但重新加载页面时出现此错误: 致命错误:在非对象上调用成员函数getImageUrl()
答案 0 :(得分:1)
尝试使用
加载您的类别Mage::getModel('catalog/layer')->getCurrentCategory()
代替。
答案 1 :(得分:1)
这应该有效:
$_helper = $this->helper('catalog/output');
$category = Mage::registry('current_category');
$_imgHtml = '';
if ($category){
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
}