2012-08-03 79 views
2

有沒有辦法在CMS頁面中按類別ID顯示類別圖像?Magento:在CMS頁面中按ID顯示類別圖像

您可以通過ID與顯示類別鏈接:

{{widget type="catalog/category_widget_link" anchor_text="Displayed Text" title="Title attribute text" template="catalog/category/widget/link/link_block.phtml" id_path="category/22"}} 

或顯示類產品通過它的ID。

{{block type="catalog/product_list" category_id="14" template="catalog/product/list.phtml"}} 

但我無法弄清楚hwo通過調用它的ID在CMS頁面中顯示特定的類別圖像。

有什麼想法?

回答

3

有貨magento你不能。

基本上有實現這一目標,按優先順序的3種方式(好到壞IMO):

  1. 直接在CMS頁面創建並使用一個widget
  2. 嵌入和使用自定義塊類型直接在一個CMS頁初始化類別
  3. 嵌入,使用芯/模板塊類型和初始化模板直接

內部類別

1.使用一個widget

我以前回答過類似的問題,並就如何建立這樣一個特定部件到一個類別一個完整的例子:

magento - category name and image in static block?


2.自定義模塊

您想創建一個帶有塊的模塊來支持 這個。使用以下語法然後你可以只包括在CMS網頁塊:

{{block type="yourmodule/category_image" category_id="14" template="yourmodule/category/image.phtml"}} 

你塊類是要看起來像這樣:

<?php 

    class Yourcompany_Yourmodule_Block_Category_Image extends Mage_Core_Block_Template 
    { 
     public function getCategory() 
     { 
      if (! $this->hasData('category')) { 
       $category = Mage::getModel('catalog/category')->load($this->getData('category_id')); 
       $this->setData('category', $category); 
      } 
      return $this->getData('category'); 
     } 
    } 

和你的模板類是要這個樣子:

<?php 

$_helper = $this->helper('catalog/output'); 
$_category = $this->getCategory(); 
$_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'); 
} 

?> 

<?php if($_imgUrl): ?> 
    <?php echo $_imgHtml ?> 
<?php endif; ?> 

3。使用核心/模板塊類型

包括像這樣的CMS頁面上..

{{block type="core/template" category_id="14" template="category/image.phtml"}} 

創建模板 - 目錄/分類/ image.phtml

<?php 
$_helper = $this->helper('catalog/output'); 
$category = Mage::getModel('catalog/category')->load($this->getData('category_id')); 
$_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'); 
} 

?> 

<?php if($_imgUrl): ?> 
    <?php echo $_imgHtml ?> 
<?php endif; ?> 
1

我不認爲你能夠調用圖像,因爲它不是一個塊。在/design/base/default/template/category/view.phtml中,它只是將圖像url作爲塊的屬性調用,然後在模板文件中正確構建輸出HTML。塊中沒有任何特定的元素可以使用magento的簡碼進行調用。

最好的辦法是建立一個新的小部件。有很多的指南,一旦你更好地瞭解它們,它們非常棒。