Magento:显示所有类别,但无法获得关联图像 [英] Magento: Displaying all categories but cannot get associating images

查看:77
本文介绍了Magento:显示所有类别,但无法获得关联图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪但很容易解决的情况.

I am having a odd but probably easily solvable situation.

我在一个代码段中包含以下代码:

I have the following code in a block:

<div class="home-categories">
    <?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>
    <?php foreach($_categories as $_category): ?>
        <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
            <img src="<?php echo $_category->getStoreIds(); ?>"/>
            <div class="category-title">
                <p><?php echo $_category->getName(); ?></p>
            </div>
        </a>
      <?php endforeach; ?>
</div>

我得到的结果HTML向我返回了所有正确的详细信息,但返回了img src.我这样做正确吗?

The resulting HTML I get returns me all the correct details but the img src. I am doing this correctly ?

推荐答案

未获得图片网址的原因是,默认情况下,Magento模型未加载所有属性.这样一来,您只需加载所需的属性,数据库查询就不会那么昂贵.以下将解决问题.

The reason you you're not getting the image url is because, by default, Magento models do not come loaded with all of the attributes. This is so you only load attributes you need and your DB queries aren't as expensive. The following will do the trick.

$_helper = Mage::helper('catalog/category');
$_categories = $_helper->getStoreCategories(false, true, false);
$_categories->addAttributeToSelect('image');

看看getStoreCategories的方法定义:

Take a a look at the method definition for getStoreCategories:

getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)

您希望该方法返回尚未加载的集合,从而返回false,true,false参数.在遍历类别之前,要确保还加载了图像属性,这就是addAttributeToSelect('image')调用的作用.

You want the method to return a collection that hasn't been loaded yet, thus the false, true, false arguments. Before you loop through the categories you want to make sure the image attribute is loaded as well, this is what the addAttributeToSelect('image') call is for.

这比$ _category-> load($ _category-> getId())更有效;因为我们没有加载整个实体.现在,当您遍历类别时,可以执行以下操作,并且应该在图像标记中包含图像URL.

This was is more efficient than $_category->load( $_category->getId()); because we are not loading the entire entity. Now when you loop through the categories you can do the following and you should have the image URL in the image tag.

<img src="<?php echo $_category->getImageUrl(); ?>"/>

这篇关于Magento:显示所有类别,但无法获得关联图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆