如何获取产品类别的网址密钥?
我可以通过$category->getName()
获取类别名称,但如果我使用此$category->getURLKey()
,则无法使用
$categories = $_product->getCategoryCollection()
->addAttributeToSelect('name');
foreach($categories as $category) {
$productCategoryName = $category->getName();
var_dump($category->getURLKey());
}
返回null
有什么想法吗?
答案 0 :(得分:1)
我不完全确定您的目标是什么,但您无法从$ _product获取类别信息。请根据我的以下代码随意提问。但我的下面的代码将抓住活动类别的孩子及其信息。但是,这应该是一个足够好的基础,无论你想做什么。
<!-- Use this section if you're trying to grab children categories and their info -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name Feel Free to remove the img src section to remove the image -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- End -->
任何问题随时都可以问!抱歉,我只是从项目中偷了一个示例,因此HTML可能没有排队直接复制。
答案 1 :(得分:1)
foreach($categories as $category) {
// for category URL key
var_dump($category->getUrlPath());
// for category URL
var_dump($category->getUrl());
}