由于某些原因,此代码在尝试获取类别子类别时返回NULL。
<?php var_dump($_category->getChildrenCategories()); ?>
以下是.phmtl文件的完整代码。
<ul id="nav_vert">
<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php if ($_category->getIsActive()) { ?>
<?php $open = $this->isCategoryActive($_category); ?>
<?php $potential = $_category->hasChildren(); ?>
<li><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo ' class="potential"'; } ?> ><?php if($potential&&$open) { echo 'v '; } elseif($potential) { echo '> '; }else{ echo ' '; }?><?php echo $_category->getName();?></a>
<?php if ($open && $potential): ?>
<?php var_dump($_category->getChildrenCategories()); ?>
<ul>
<?php foreach ($_category->getChildrenCategories() as $subcategory): ?>
<?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?>
<?php $open = $this->isCategoryActive($subCat); ?>
<?php $potential = $subCat->hasChildren(); ?>
<li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if($potential) { echo ' class="potential"'; } ?><?php if(!$potential&&$open) { echo ' class="final"'; } ?> ><?php if($potential&&$open) { echo ':: '; } elseif($potential) { echo '> '; }?><?php echo $subCat->getName(); ?> (<?php echo $subCat->getProductCount(); ?>)</a>
<?php if ($open && $potential): ?>
<ul>
<?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?>
<?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?>
<?php $open = $this->isCategoryActive($subsubCat) ?>
<li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?php if($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?> (<?php echo $subsubCat->getProductCount(); ?>)</a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php } ?>
<?php endforeach ?>
</ul>
此行始终返回true
<?php $potential = $_category->hasChildren(); ?>
我知道该类别有孩子。
有谁能说明为什么这不起作用?
这就是我将phtml放在页面中的方式:
<reference name="left">
<block type="catalog/navigation" name="catalog.vertnav" template="catalog/navigation/vert_nav.phtml" before="-" />
</reference>
Magento 1.5.1.0版
答案 0 :(得分:2)
尝试使用$_category->getChildren()
(而不是$_category->getChildrenCategories()
)
有一个轻松的一天,
逾越节
答案 1 :(得分:0)
您可能也想试试这个,
foreach ($_category->getCategories($_category) as $subcategory):
getCategories()
是一种更复杂的形式,但它也允许您进行更多控制。通过指定递归级别,您不仅可以检索孩子,还可以检索孙子......
答案 2 :(得分:0)
尝试使用此代码,
$this->getCurrentCategory()->getChildrenCategories()
希望这有帮助
答案 3 :(得分:0)
<?php $helper = $this->helper('catalog/category') ?>
<?php $categories = $this->getStoreCategories() ?>
<?php foreach($categories as $category): ?>
<?php $subcategories = $category->getChildren() ?>
<?php foreach($subcategories as $subcategory): ?>
<?php $subsubcategories = $subcategory->getChildren() ?>
<?php foreach($subsubcategories as $subsubcategory): ?>
<?php endforeach; ?><!-- end foreach subsubcategories -->
<?php endforeach; ?><!-- end foreach subcategories -->
<?php endforeach; ?><!-- end foreach categories -->
这是提取儿童类别背后的基本思想 因此,你可以相应地工作。