gpt4 book ai didi

php - 简单调用以获取类别图像并显示在列表中

转载 作者:可可西里 更新时间:2023-10-31 23:57:23 26 4
gpt4 key购买 nike

我正在按父类别 ID 显示子类别列表,并希望显示类别图像来代替类别名称。

这是我目前所拥有的...

<div id="menu_brands">
<div class="brand_head">
<h3><?php echo $this->__('Browse By Brand') ?></h3>
</div>
<div class="brand_list">
<?php
$cats = Mage::getModel('catalog/category')->load(6)->getChildren();
$catIds = explode(',',$cats);

$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = $category->getUrl();
$img = $category->getImageUrl(); //I suspect this line is wrong
}

ksort($categories, SORT_STRING);
?>

<ul>
<?php foreach($categories as $name => $url): ?>
<li>
<!--<a href="<?php echo $url; ?>"><?php echo $name; ?></a>-->
<a href="<?php echo $url; ?>" title="<?php echo $name; ?>">
<img src="<?php echo $img; ?>" width="auto" alt="<?php echo $name; ?>" /> <!--I suspect this line is wrong-->
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>

我已经尝试了无数种方法来显示图像来代替类别名称,但似乎没有任何方法可以显示图像。目前,对于上述内容,输出是一个空的“img src”,因此我正在尝试的内容显然存在错误(并且可能是实现我所追求的目标的更好方法)。

请哪位好心人指出问题所在?

如果它有任何相关性,我打算之后做的是以网格格式显示类别图像(每行 3 或 4 个)。

非常感谢。

最佳答案

zigojacko 的解决方案并不理想,因为它在循环中单独加载模型。这不能很好地扩展,并且有很多类别会破坏您的数据库。理想情况下,您会将图像添加到子集合中。

更快的解决方案是将图像属性添加到带有 ID 过滤器的集合中,例如 B00MER:

// Gets all sub categories of parent category 'Brands'
$parent = Mage::getModel('catalog/category')->load(6);

// Create category collection for children
$childrenCollection = $parent->getCollection();
// Only get child categories of parent cat
$childrenCollection->addIdFilter($parent->getChildren());
// Only get active categories
$childrenCollection->addAttributeToFilter('is_active', 1);

// Add base attributes
$childrenCollection->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->setOrder('position', Varien_Db_Select::SQL_ASC)
->joinUrlRewrite();

// ADD IMAGE ATTRIBUTE
$childrenCollection->addAttributeToSelect('image');

?>
<ul>
<?php foreach($childrenCollection as $cat): ?>
<li>
<a href="<?php echo $cat->getURL(); ?>" title="<?php echo $cat->getName(); ?>">
<img class="cat-image" src="<?php echo $cat->getImageUrl(); ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>

关于php - 简单调用以获取类别图像并显示在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10009012/

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