2

有没有办法通过 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"}}

但我无法弄清楚如何通过调用它的 id 在 CMS 页面中显示特定类别的图像。

任何的想法?

4

2 回答 2

3

Magento 有货,你不能。

基本上有 3 种方法可以实现这一点,按优先顺序排列(从好到坏 imo):

  1. 创建和使用小部件
  2. 直接嵌入 cms 页面并使用自定义块类型来初始化类别
  3. 直接嵌入到cms页面,使用core/template块类型,直接在模板内初始化category

1.使用小部件

我之前回答过一个类似的问题,并提供了一个完整的例子来说明如何构建一个特定于某个类别的小部件:

magento - 静态块中的类别名称和图像?


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; ?>
于 2012-08-03T07:28:11.493 回答
1

我认为您不能只调用图像,因为它不是块。在 /design/base/default/template/category/view.phtml 中,它只是调用图像 url 作为块的属性,然后在模板文件中构建输出 HTML。块中没有任何特定元素可以使用 magento 的简码调用它。

最好的办法是构建一个新的小部件。有很多指南,一旦您更好地了解它们,它们就会非常棒。

于 2012-08-03T01:52:45.120 回答