0
votes

To preface this question, I am a front-end developer with limited knowledge of php. I am attempting to show all categories and sub-categories on the left navigation in Magento. I created a phtml file using a method I found on GitHub. This works great to pull in the top level of categories but I want to show the subcategories too. Here's the code I have now:

<?php $_categories=$this->getCurrentChildCategories() ?>

<?php if($_categories->count()): ?>
<ul class="category-links"> 
<?php foreach ($_categories as $_category): ?>
    <?php if($_category->getIsActive()): ?>
    <li class="<?php echo $this->htmlEscape($_category->getUrlKey()) ?>">
       <a href="<?php echo $this->getCategoryUrl($_category) ?>">
          <?php echo $this->htmlEscape($_category->getName()) ?>
       </a>
    </li>
    <?php endif; ?>
    <?php endforeach ?>
</ul>
<? endif; ?>

This pulls in the categories like this:

Category 1 Category 2 Category 3

But what I want is this

Category 1 Subcategory 1 of Category 1 Subcategory 2 of Category 1 Category 2 Subcategory 1 of Category 2 Subcategory 2 of Category 2

and so on...

Can anyone help me with this?

Thanks so much!

1

1 Answers

2
votes

Something like this. You need to check it because I didn't try it, but maybe it will get you on the right track.

<?php $_categories=$this->getCurrentChildCategories() ?>

<?php if($_categories->count()): ?>
<ul class="category-links"> 
<?php foreach ($_categories as $_category): ?>
    <?php if($_category->getIsActive()): ?>
    <li class="<?php echo $this->htmlEscape($_category->getUrlKey()) ?>">
       <a href="<?php echo $this->getCategoryUrl($_category) ?>">
          <?php echo $this->htmlEscape($_category->getName()) ?>
       </a>
    </li>
    <?php $_subcategories = $_category->getChildrenCategories();
    foreach ($_subcategories as $_subcategory):?>
    <li class="<?php echo $this->htmlEscape($_subcategory->getUrlKey()) ?>">
       <a href="<?php echo $this->getCategoryUrl($_subcategory) ?>">
          <?php echo $this->htmlEscape($_subcategory->getName()) ?>
       </a>
    </li>
    <?php endforeach; ?>
    <?php endif; ?>
    <?php endforeach ?>
</ul>
<? endif; ?>