Get Child Categories in Magento using getChildCategories()

ini_set('display_errors', '1');
error_reporting(E_ALL);

// include magento goodies
require '../app/Mage.php';

Mage::app();

//$currentCategory = Mage::registry('current_category');

$id = 4; //  represents the current category id for my test

$currentCategory = Mage::getModel('catalog/category')->load($id);

$helper = Mage::helper('catalog/category');

if( is_null( $currentCategory ) ) {
	echo '$currentCategory is null';
} else {

	// LEVEL 1
	echo $currentCategory->getName() . '<br>' . PHP_EOL;

	$level_1_children = $currentCategory->getChildrenCategories();

	foreach ( $level_1_children as $level_2 ) {
		// LEVEL 2
			echo '-' . $level_2->getName() . '<br>' . PHP_EOL;
			$level_2_children = $level_2->getChildrenCategories();
			foreach ( $level_2_children as $level_3 ) {
				// LEVEL 3
				$level_3->load();
				if( $level_3->getIncludeInMenu() ) {
					echo '--' . $level_3->getName() . '<br>' . PHP_EOL;
				} else {
					echo '--<span style="color:magenta">' . $level_3->getName() . '</span><br>' . PHP_EOL;
				}
			}
	}
}

Fatal error: Call to a member function getHelperClassName() on a non-object

The Problem:

I’m writing a Magento script to display categories in a custom navigation and was getting this error:

Fatal error: Call to a member function getHelperClassName() on a non-object in /var/www/vhosts/dev-site/public_html/app/Mage.php on line 546

 

The Solution:

Mage:app();

 

My Code:

<?php

ini_set('display_errors', '1');
error_reporting(E_ALL);

// include the Magento goodness
require '../app/Mage.php';

Mage::app();

$id = 4; //  category id for this test
?>

<?php $helper = Mage::helper('catalog/category') ?>
<?php $categories = $helper->getStoreCategories() ?>
<?php if (count($categories) > 0): ?>
    <ul class="top-level-category">
        <?php foreach($categories as $category): ?>
            <li>
                <a href="<?php echo $helper->getCategoryUrl($category) ?>">
                    <?php echo $category->getName() ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>