I am trying to get only parent categories of a category id 4 but right now instead of parent category it is listing all child categories. Below is the code that I currently have in .phtml template file. Is there something wrong in the code? How could I achieve it?
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = Mage::getModel('catalog/category')->getCategories(4); ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<?php foreach($_categories as $_category): ?>
<?php
//arrange by Letter
foreach($_category->getChildren() as $_sub){
$letter = substr(strtolower($_sub->getName()),0,1);
$subCategories[$letter][0]['name'] = $letter; //0 index is the letter
$i = 1;
while(isset($subCategories[$letter][$i])){
$i++;
}
$subCategories[$letter][$i]['name'] = $_sub->getName();
$subCategories[$letter][$i]['url'] = $_helper->getCategoryUrl($_sub);
}
?>
<?php endforeach ?>
<?php
$i = 0;
$x = 0;
$d = 0;
$splits = 1;
foreach($subCategories as $sub){
$d += count($sub); //split it into 3rds
}
$d = round($d / 5);
//get a count of all elements for splitting
ksort($subCategories);
foreach($subCategories as $sub){
$i = 0;
//$d = round(count($sub) / 3); //split it into 3rds
echo '<div class="catContainer">';
asort($sub);
$displayHead = TRUE;
while(isset($sub[$i])){
if($x % $d == 0 && $x != 0 && $splits != 5){
$splits++;
echo '</div></div><div class="containerContainer"><div class="catContainer">';
if($i != 0){
echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
}
}
if($i == 0){
echo '<div class="catContainerTop">'.$sub[0]['name'].'</div>';
} else if($i != 0){
echo '<span><a href="'.$sub[$i]['url'].'">' . $sub[$i]['name'] . '</a></span><br />';
}
$i++;
$x++;
}
echo '</div>';
}
?>
<?php endif; ?>
Update:
I did try adding <?php $subCategories = array(); ?>
before <?php if (count($_categories) > 0): ?>
as suggested but it is not helping at all so anyone with the suggestion and solutions?
<?php $subCategories = array(); ?>
before<?php if (count($_categories) > 0): ?>
and it didn't work.