gpt4 book ai didi

Magento:在 CMS 页面中按 ID 显示类别图像

转载 作者:行者123 更新时间:2023-12-04 18:15:36 24 4
gpt4 key购买 nike

有没有办法通过 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 页面中显示特定类别的图像。

任何想法?

最佳答案

Magento 有货,你不能。

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

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


  • 1.使用小部件

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

    magento - category name and image in static block?

    2.自定义模块

    您需要创建一个带有 block 的模块来支持这一点。然后,您可以使用以下语法将 block 包含在 cms 页面中:
    {{block type="yourmodule/category_image" category_id="14" template="yourmodule/category/image.phtml"}}

    您的 block 类将如下所示:
    <?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.使用核心/模板 block 类型

    像这样包含在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; ?>

    关于Magento:在 CMS 页面中按 ID 显示类别图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11787545/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com