0

I have a section of code which finds if there are subcategories of a category and if there is then it displays the subcategories in a list view.

I am trying to add in a function that says if the subcategories are over 1 then show a DIV and if not then do not show the DIV as it wont be showing any subcategories.

This is what I have so far but it doesn't seem to be working.

<?php 
/* Load category by id */
$cat = Mage::registry('current_category');

/*Returns comma separated ids*/
$subcats = $cat->getChildren();

if (count($subcats) > 0): ?>

<div style="width:100%; height:auto; text-align:center; font-size:16px; color:#373737; padding:2px 0; margin-top:-5px; margin-bottom:3px; background:#f1f1f1;">Choose Your Category</div>

<?php endif;

foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
  echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a></li>';

/* Load category by id */
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());

}
}
?> 
4

2 回答 2

1
if (count($subcats) > 0) 

我相信你的 $subcats 不是数组它的字符串,所以你应该使用 strlen 函数

if(strlen(trim($subcats)) > 0)
于 2012-09-20T08:40:28.517 回答
0

也许你应该这样做:

$subcats = explode(',', $cat->getChildren());

前 :

if (count($subcats) > 0)

因为你的 $subcats 似乎是一个 csv 字符串,然后替换你的:

foreach(explode(',',$subcats) as $subCatid)

经过 :

foreach ($subcats as $subCatid)
于 2012-09-20T09:01:51.103 回答