【问题标题】:Magento - All categories have same imageMagento - 所有类别都有相同的图像
【发布时间】:2012-11-27 04:16:05
【问题描述】:

所以有一些代码可以获取单个子类别的图像:

foreach ($collection as $cat){
    $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
    $layer = Mage::getSingleton('catalog/layer');   
    $layer->setCurrentCategory($cur_category);
    $_img = $this->getCurrentCategory()->getImageUrl();
}

$cat 是我想要获取图像的子类别。我遇到的问题 - 子类别的图像都是一样的!子类别中的第一个图像是显示在页面上的图像。链接和名称都是正确的:(这也在 for 循环中)

<div>
    <a href="<?php echo $helper->getCategoryUrl($cat);?>">
        <img src="<?php echo $_img; ?>" title="<?php $cat->getName() ?>"/>
        <cite><?php echo $cat->getName();?></cite>
    </a>
</div>

我正在catalog/category/view.phtml 中进行此修改。它比显示的要多一些,但这就是所有相关信息。

编辑

所有类别都有自己独特的图片,这些图片已正确上传。它们正确显示在管理员中。此外,$cat-&gt;getId() 正在返回各个子类别的正确 ID。

【问题讨论】:

    标签: image magento categories catalog


    【解决方案1】:

    @RandyHall,您确定 $this->getCurrentCategory() 将从与 $layer->setCurrentCategory($cur_category); 相同的位置获取类别吗?将其设置为?

    观看源代码herehere。 如您所见,类别设置为层模型并从注册表获取返回类别(通过调用块)。

    所以我建议你这样做:

    $_img = $layer->getCurrentCategory()->getImageUrl();
    

    【讨论】:

      【解决方案2】:

      也许你在获取图片 url 时做错了。

      而不是使用

      $_img = $this->getCurrentCategory()->getImageUrl(); 
      

      试试下面的代码

      $_img = $cur_category->getImageUrl();
      

      【讨论】:

      • 阿南特是对的。因为 $cur_category 是您循环中的变量。 $this->getCurrentCategory() 将从注册表返回值。
      • @JevgeniSmirnov 这个想法是(根据我所阅读的内容)将当前类别设置为您尝试为其获取图像的类别,然后获取当前图像。
      • @Anant 这也不起作用......它只是不返回任何东西。也试过$cat-&gt;getImageUrl();,也没什么。
      • @RandyHall 您使用什么代码加载类别集合? (即:$collection)
      【解决方案3】:
      foreach ($collection as $cat){
          $cur_category = Mage::getModel('catalog/category')->load($cat->getId());
          $_img = $cur_category->getImageUrl();
      }
      

      在我看来这就是你想要的?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-20
        • 1970-01-01
        • 2021-07-03
        • 2013-12-10
        • 2016-08-22
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多