当前类别id和title

$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();
Mage::registry('current_category')->getName();

The majority of Magento websites out there list their top level categories as well as the current categories sub-categories. This feature is commonly requested on forums so I decided to write a small post about it.

Rather than just write out the code, I will show you a few variations so that you can get the right one for you.

All of the following code samples can be copy and pasted into ANY template file and will function correctly.

Display Top Level Categories Only

<?php
/** http://fishpig.co.uk - Magento Tutorials** Display top level categories*
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?><ul><?php foreach($_categories as $_category): ?><li><a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a></li><?php endforeach; ?></ul>
<?php endif; ?>

Display Top Level Categories and ALL Subcategories

<?php
/** http://fishpig.co.uk - Magento Tutorials** Display top level categories and subcategories*
**/
?>
<?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; ?>

Display Top Level Categories and Current Categories SubCategories

<?php
/** http://fishpig.co.uk - Magento Tutorials** Display top level categories and* subcategories of the current category*
**/
?>
<?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 &amp;&amp; $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; ?>

来源: http://fishpig.co.uk/blog/display-categories-and-subcategories-in-magento.html

其他方法:

1.

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');foreach ($categories as $category)
{if ($category->getIsActive()) { // Only pull Active categories$entity_id = $category->getId();$name = $category->getName();$url_key = $category->getUrlKey();$url_path = $category->getUrl();}
}

2.

<?php // load(level-number)?>
<?php $categoryIds = Mage::getModel('catalog/category')->load(3)->getChildren() ?>
<?php $categoryIds = explode(',', $categoryIds); ?>
<?php $count = count($categoryIds); $i=0; ?>
<?php foreach ($categoryIds as $categoryId): ?>
<?php $category = Mage::getModel("catalog/category")->load($categoryId) ?>
<li<?php if (++$i == $count): ?> class="last"<?php endif ?>><a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()) ?>"><?php echo $this->stripTags($category->getName()) ?></a></li>
<?php endforeach ?>

magento 获取类别和子类别 Display Categories and SubCategories in Magento相关推荐

  1. magento 获取 pages/Static Block 内容 Get Pages/Static Block in Magento

    1. pages Sometimes, you want to use information like title or content from a CMS page (or multiple C ...

  2. Magento: 获取类别所有子类别 (无限级别-目录树) Get All Sub Categories

    生成分类目录树(Category Tree) $rootcatId = Mage::app()->getStore()->getRootCategoryId(); $categories ...

  3. Magento 获取当前店铺信息(首页,类别,地址等)

    众所周知,Magento的多店铺功能非常强大,通过多店铺功能,我们可以在一个Magento应用当中创建在外观上看来完全不相关联的多个店铺,并且可以绑定不同的域名.在Magento的开发当中,经常需要获 ...

  4. Revit二次开发之获取参数绑定的类别【比目鱼原创】

    项目参数保存在Revit 里,所以删除共享参数或者共享参数文件,都不会对项目参数产生影响,哪怕项目参数是通过共享参数创建的.项目参数在创建的时候,就已经和类别绑定了.也就是说和类别对应的元素都加上了新 ...

  5. Magento 获取有效属性 Display available options for attributes of Configurable

    1. 获取全部 //get Product $cProduct = Mage::getModel('catalog/product')->load($_product->getId()); ...

  6. 01-创建项目 创建数据表-类别管理--添加类别--持久层

    文章目录 1. 关于此项目 2. 关于项目的开发流程 3. 创建数据库与数据表 4. 关于Project与Module 5. 创建Project 6. 商品管理模块项目 7. 关于编写代码 8. 类别 ...

  7. 语义分割中单类别和多类别图片数据标注,以及灰度类别转换

                                                                                                        ...

  8. python多线程返回值_python获取多线程及子线程的返回值

    最近有个需求,用多线程比较合适,但是我需要每个线程的返回值,这就需要我在threading.Thread的基础上进行封装 import threading class MyThread(threadi ...

  9. Magento: 获取产品评论 get all reviews with review summary

    1. 根据产品id获取该产品评论 $productId = 1234; $product = Mage::getModel('catalog/product')->load($productId ...

最新文章

  1. 哪些“双一流”更得华为偏爱?这所高校人数远超清北华五
  2. 用于自然语言处理的卷积网络
  3. 刷题总结——魔法森林(bzoj3669)
  4. 蓝桥杯【(经验+资源)分享】【从零备考 蓝桥杯】说说我的经历及备考经验
  5. android 项目部署,Android发布项目到JCenter
  6. 用注解方式写定时任务
  7. php基础知识 书写格式
  8. 如何补充策略与模型在风控中的滞后性
  9. 从大到小依次输出三个整数
  10. Access to the path ‘‘ is denied.解决方案
  11. 可视化理解卷积神经网络 - 反卷积网络 - 没看懂
  12. 二叉搜索树与双向链表(C++)
  13. bp神经网络回归预测模型(python实现)_bp神经网络预测代码python
  14. 伽罗华有限域_伽罗华域性质简析
  15. 大数据处理中心什么意思_什么是数据处理中心或数据中心
  16. ctab提取dna流程图_DNA提取(CTAB法)
  17. 【转载】不用加号和减号计算两个数的加法
  18. STM32 W5500以太网通讯
  19. 通过rvm 安装 ruby
  20. GB/T35590-2017移动电源CMA检测报告,移动电源新国标检测

热门文章

  1. A+B Problem(洛谷-P1001)
  2. 信息学奥赛C++语言:换钱
  3. 影响程序性能的几个关键因素
  4. glob.glob() + os.path.join() :找到文件路径,拼接路径
  5. 【6.1】图片风格迁移 Neural Style Transfer
  6. SinGAN: Learning a Generative Model from a Single Natural Image
  7. Array,List,数据类型,caseclss模式匹配
  8. CreateJS基础 学习笔记(上)
  9. vscode设置缩进2个空格
  10. 网页版进销存源码ERP多仓库管理系统源码