我正在建立一个 Magento 商店,并希望能够显示类别列表并将每个类别链接到其自己的页面。
我有一个 ID 为 42 的“品牌”类别,我想显示一个子类别列表,并确保每个子类别都链接到 CMS 中指定的 URL 键。
有没有人有过使用 Magento 做这件事的经验?
我正在建立一个 Magento 商店,并希望能够显示类别列表并将每个类别链接到其自己的页面。
我有一个 ID 为 42 的“品牌”类别,我想显示一个子类别列表,并确保每个子类别都链接到 CMS 中指定的 URL 键。
有没有人有过使用 Magento 做这件事的经验?
如果您喜欢编辑主题,此代码片段将为您提供当前类别的所有子类别的列表(来自会话,因此这应该适用于您的主题中的任何位置)。我通常在 app/design/frontend/default/ theme_name /template/catalog/category/view.phtml中使用它
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat):?>
<?php if($_category->getIsActive()):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getImageUrl();
?>
<li>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>"/>
<cite><?php echo $cat->getName();?></cite>
</a>
</li>
<?php endif?>
<?php endforeach;?>
</ul>
如果您想显示顶级类别和子类别,您可以这样做。
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
要显示顶级类别和当前类别子类别,您可以这样做......
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
<?php if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<?php echo $_subcategory->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
这个问题需要很长的答案。我会给你指出正确的地方。
1) 最好的解决方案是使用免费扩展。我没有尝试过,但它会适合目的。您将不得不做一些 CSS 来获得正确的外观和感觉。
http://www.magentocommerce.com/extension/1562/magento-easy-catalog-images 演示:http ://extension01.templates-master.com/gb/electronics.html
2)我不信任模块,因为如果供应商决定停止支持它,升级可能会变得困难。我已使用以下论坛主题中的信息创建了一个 vew 站点。看看......可能不是直截了当。您可能必须将核心文件的一些副本复制到本地目录。
http://www.magentocommerce.com/boards/viewthread/3770/P30/
希望这会对您有所帮助:)
在查看了 magento 网站上的所有解决方案后,我发现上述 wookiehangover 的解决方案有效,并且需要大约 8 秒的时间来实施。
创建一个您可以设置样式的 UL。谢谢。
创建静态块后,您可以通过此脚本获取任何子类别列表:
$_helper = Mage::helper('catalog/category');
$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) <= 0) { return; }
$count = 0;
foreach($_subcategories as $_category) {
$category = Mage::getModel('catalog/category')->load($_category->getId());
$ret->{"object_".$count} ->url = $_helper->getCategoryUrl($_category);
$ret->{"object_".$count} ->name = $_category->getName();
$ret->{"object_".$count} ->id = $_category->getId();
$ret->{"object_".$count} ->image = $category->getImageUrl();
$count++;
}
return $ret;
}
$list = list_subcategories(5);
echo "<pre>"; print_r($list); echo "</pre>";
?>
I made this little video on how I create custom category listing blocks with Magento. I am sure there are better ways of achieving this or even something I could have done better, but it’s just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Block
Thanks!
如何仅列出属于当前项目的类别。并非页面上的所有类别。
但在树状视图中。
类别 - 子类别 1 类别 2 - 子类别 1 - 子类别 1
BR Cveto