1

カテゴリ テンプレート (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() への呼び出し

4

2 に答える 2

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');
    }
}
于 2013-07-09T07:58:14.607 に答える
1

カテゴリをロードしてみてください

Mage::getModel('catalog/layer')->getCurrentCategory()

代わりは。

于 2013-07-09T04:55:49.993 に答える