Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can get all categories and subcategories?

How I can get all categories and subcategories if the category is active, but "Include in Navigation Menu" is set to "No"?

I try to use this:

<?php 
$_categories = Mage::getBlockSingleton('catalog/navigation'); 
foreach ($_categories->getStoreCategories() as $_category) { 
$category = Mage::getModel('catalog/category'); 
$category->load($_category->getId()); 
$subcategories = explode(',', $category->getChildren()); 
?> 
<dl> 
<dt><?php echo $this->htmlEscape($_category->getName()); ?></dt> 
<dd> 
<ol> 
<?php 
foreach ($subcategories as $subcategoryId) { 
$category->load($subcategoryId); 
echo '<li><a href="' . $category->getURL() . '">' . $category->getName() . '</a></li>'; 
} 
?> 
</ol> 
</dd> 
</dl> 
<?php

} 
?> 

But if a category's “Include in Nav menu" is "No”, it won't show on the front page!

like image 201
Donnie Avatar asked Jan 11 '13 14:01

Donnie


People also ask

How do I display all subcategories from a specific category in WooCommerce?

Click on Appearance > Customize. Then go to WooCommerce > Product Catalog. Select “show subcategories” from Category Display. Click on Save Changes.

How do I show all the categories on a WordPress page?

In menus, go to Appearance → Menus, select categories and click Add to Menus. In the sidebar, go to Appearance → Widgets, then choose the categories that you want to appear in the sidebar and click Add Widget. When you want to show subcategories in the sidebar, drag and drop categories to a Sidebar.


1 Answers

You only need to change one thing! When you call $_categories = Mage::getBlockSingleton('catalog/navigation') you're actually grabbing the categories from the catalog/navigation model specifically - the filtering out of "non navigation" categories is already complete. Instead, we can grab a collection from the catalog/category model to make sure we get all categories available on the site:

$categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addIsActiveFilter();

Note that I am using addIsActiveFilter() to make sure we only get categories that are currently active / enabled.

like image 123
1000Nettles Avatar answered Sep 19 '22 18:09

1000Nettles