I have a file that is successfully looping through my categories and subcategories. I am successfully able to echo all the categories and their links onto this page
http://firstaidmart.com/list-all-categories.html
BUT
I do not see why these (keywords and description) are not echoing
<?php echo htmlspecialchars($this->getKeywords()) ?>
<?php echo htmlspecialchars($this->getDescription()) ?>
The file is located here app/design/frontend/mystoretheme/default/template/catalog/category/listofcats.phtml
Then im placing it on a block in a cms page {{block type="catalog/navigation" name="catalog.category" template="catalog/category/listofcats.phtml"}}
The goal is to be able to display each of the categories list of keywords and their descriptions within the same < li> and loop giving me a list like this
- Category
- Keywords
- Description
Here is my code. I have omitted my attempts at keyword and description since they are not working.
<div class="block block-list block-categories">
<div id="block-categories" class="block-title active">
 <strong><span>Categories </span></strong>
</div> 
<div id="leftnav" class="block-content" style="display:block">
 <?php $helper = $this->helper('catalog/category') ?>
 <?php $categories = $this->getStoreCategories() ?>
 <?php if (count($categories) > 0): ?>
 <ul id="leftnav-tree" class="level0">
 <?php foreach($categories as $category): ?>
 <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
 <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
 <?php //if ($this->isCategoryActive($category)): ?>
 <?php $subcategories = $category->getChildren() ?>
 <?php if (count($subcategories) > 0): ?>
 <ul id="leftnav-tree-<?php echo $category->getId() ?>" class="level1">
 <?php foreach($subcategories as $subcategory): ?>
 <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
 <a href="<?php echo $helper->getCategoryUrl($subcategory) ?>"><?php echo $this->escapeHtml(trim($subcategory->getName(), '- ')) ?></a>
 <?php $secondLevelSubcategories = $subcategory->getChildren() ?>
 <?php if (count($secondLevelSubcategories ) > 0): ?>
 <ul id="leftnav-tree-<?php echo $subcategory->getId() ?>" class="level2">
 <?php foreach($secondLevelSubcategories as $secondLevelSubcategory ): ?>
 <li class="level2<?php if ($this->isCategoryActive($secondLevelSubcategory )): ?> active<?php endif; ?>">
 <a href="<?php echo $helper->getCategoryUrl($secondLevelSubcategory ) ?>"><?php echo $this->escapeHtml(trim($secondLevelSubcategory ->getName(), '- ')) ?></a>
 </li>
 <?php endforeach; ?>
 </ul>
 <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
 <?php endif; ?>
 <?php endforeach; ?>
 </ul>
 <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
 <?php endif; ?>
 <?php //endif; ?>
 </li>
 <?php endforeach; ?>
 </ul>
 <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
 <?php endif; ?>
</div>
- 
 Any updates on this? Does it work for you?sv3n– sv3n2017年06月13日 11:54:35 +00:00Commented Jun 13, 2017 at 11:54
- 
 Hi sorry I haven't been able to try it out i will next week and I'll let you know! Thank you!monjexpress– monjexpress2017年06月16日 23:34:00 +00:00Commented Jun 16, 2017 at 23:34
- 
 I am going to test by Friday and let you know. Thank you!!monjexpress– monjexpress2017年07月05日 18:08:55 +00:00Commented Jul 5, 2017 at 18:08
1 Answer 1
It does not work, because description and meta_keywords aren't part of the category-tree collection.
Load order:
- Mage_Catalog_Block_Navigation::getStoreCategories()
- Mage_Catalog_Helper_Category::getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
- Mage_Catalog_Model_Category::getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
- Mage_Catalog_Model_Resource_Category::getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
- Varien_Data_Tree_Dbp::loadNode($parent)
At step 4 $tree->addCollectionData(null, $sorted, $parent, $toLoad, true); is called, where the first parameter is a collection ... see:
// Mage_Catalog_Model_Resource_Category_Tree
public function addCollectionData($collection = null, $sorted = false, $exclude = array(), $toLoad = true,
 $onlyActive = false)
{
 if (is_null($collection)) {
 $collection = $this->getCollection($sorted);
 } else {
 $this->setCollection($collection);
 }
If you want additional attributes loaded, you have to set your own collection.
I think the easiest way is to create a extension, with a helper method, that do this:
app/code/local/My/Module/Helper/etc/config.xml
<?xml version="1.0"?>
<config>
 <modules>
 <My_Module>
 <version>1.0.0</version>
 </My_Module>
 </modules>
 <global>
 <helpers>
 <my_module>
 <class>My_Module_Helper</class>
 </my_module>
 </helpers>
 </global>
</config>
app/code/local/My/Module/Helper/Data.php
public function getStoreCategories()
{
 $parent = Mage::app()->getStore()->getRootCategoryId();
 $recursionLevel = max(0, (int) Mage::app()->getStore()->getConfig('catalog/navigation/max_depth'));
 $tree = Mage::getResourceModel('catalog/category_tree');
 /* @var $tree Mage_Catalog_Model_Resource_Category_Tree */
 $nodes = $tree->loadNode($parent)
 ->loadChildren($recursionLevel)
 ->getChildren();
 $collection = $tree->getCollection()
 ->addAttributeToSelect('description')
 ->addAttributeToSelect('meta_keywords');
 $tree->addCollectionData($collection, false, $parent);
 return $nodes;
}
in your template file
Change:
<?php $categories = $this->getStoreCategories() ?>
To:
<?php $categories = $this->helper('my_module')->getStoreCategories()) ?>
- 
 @monjexpress please accept if solved. :)sv3n– sv3n2018年01月11日 19:29:12 +00:00Commented Jan 11, 2018 at 19:29