Magento categories in WordPress

26.Oct.2010

Before reading this article it is advised to read “How to run Magento in WordPress“, as it could help you overcome some basic problems that might emerge.

This snippet shows you how to get Magento categories and show them in WordPress so you can use them as a menu. Copy following snippet into your WordPress functions.php file, and call function getMagentoCategories() inside your sidebar or menu in order to get categories from Magento inside your WordPress instalation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function getMagentoCategories() {
	$mageFilename = realpath('../shop/app/Mage.php');
	require_once( $mageFilename );
	umask(0);
 
	Mage::app();
	Mage::getSingleton('core/session', array('name'=>'frontend'));
 
	$categories = Mage::helper('catalog/category')->getStoreCategories();
 
	echo '<ul class="level0">';
	foreach ($categories as $_category) {
		echo '<li class="level0"><a href="/shop/' . $_category->getUrlKey() . '.html"><span>' . $_category->getName() . '</span></a>';
		if($_category->hasChildren()) {
			echo '<ul class="level1">';
			foreach($_category->getChildren() as $subcategory) {
				if($subcategory->getIsActive()) { // check if category is active
					echo '<li class="level1"><a href="/shop/' . $_category->getUrlKey() . '/' . $subcategory->getUrlKey() . '.html"><span>' . $subcategory->getName() . '</span></a></li>';
				}
			}
			echo '</ul>';
		}
		echo '</li>';
	}
	echo '</ul>';
}

Notice: This script won’t show categories in all levels but only first two.