Create custom category navigation wordpress

I want to create a custom category navigation 3 level in wordpress.
I don’t want to use php wp_nav_menu(), because i need to add cateogry thumbnail inside the navigation.
I found this code :

<?php // Create category navigation with posts for each subcat
    $categories = get_categories();
    $catID = $cat->cat_ID;
    foreach ($categories as $cat) {
        if($cat->category_parent == 0){ //If is a top level category
            $subcategories = get_categories('child_of='. $cat->cat_ID);
            if (count($subcategories)>0){ ?>

                <li class="haschild"><a><?php echo $cat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></a>

                <?php } /* end if has subcats */
            else { // else; does not have childen ?>

                <li><a href="<?php echo get_category_link( $cat->cat_ID); ?>"><?php echo $cat->name; ?></a><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></li>

            <?php } //end else ?>
        <?php // Get the subcats !!! THIS RETURNS ALL DECENDING LEVELS. 
            $subcategories = get_categories('child_of='. $cat->cat_ID);
            if (count($subcategories)>0){
                echo "<ul class='sub-menu'>";
                foreach ($subcategories as $scat) { ?>

                <li><a href="<?php echo get_category_link( $scat->cat_ID); ?>"><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></span></a></li>



            <?php
            echo "";
             }
            echo "</ul> </li>"; // Close the Child UL DIV and parent LI
            }   //endif has subcats ?>

        <?php } //end if parent ?>
    <?php } //end foreach ?>

Read More

But it doesn’t work on 3 level, just on 2 level.
you can look the menu directly on my website : http://www.designsd.fr/ap/

Do you have an idea to change it ?

Related posts

1 comment

  1. Here is the updated code with a new loop level:

    <?php // Create category navigation with posts for each subcat
    $categories = get_categories();
    $catID = $cat->cat_ID;
    foreach ($categories as $cat) {
        if($cat->category_parent == 0){ //If is a top level category
            $subcategories = get_categories('child_of='. $cat->cat_ID);
            if (count($subcategories)>0){ ?>
    
            <li class="haschild"><a><?php echo $cat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a></span>
    
                <?php } /* end if has subcats */
            else { // else; does not have childen ?>
    
                <li><a href="<?php echo get_category_link( $cat->cat_ID); ?>"><?php echo $cat->name; ?></a><span class="image"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></span></li>
    
            <?php } //end else ?>
        <?php // Get the subcats !!! THIS RETURNS ALL DECENDING LEVELS. 
            $subcategories = get_categories('child_of='. $cat->cat_ID);
            if (count($subcategories)>0){
                echo "<ul class='sub-menu'>";
                foreach ($subcategories as $scat) {
                    $child_subcategories = get_categories('child_of='. $scat->cat_ID);
                    if (count($child_subcategories)>0){ ?>
                        <li class="test"><a><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></span></a>
                    <?php } else { ?>
                        <li><a href="<?php echo get_category_link( $scat->cat_ID); ?>"><?php echo $scat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($scat->term_id); ?>" /></a></span>
                    <?php }
                    if (count($child_subcategories)>0){
                        echo "<ul class='sub-menu'>";
                        foreach ($child_subcategories as $sscat) { ?>
                            <li><a href="<?php echo get_category_link( $sscat->cat_ID); ?>"><?php echo $sscat->name; ?><span class="image"><img src="<?php echo z_taxonomy_image_url($sscat->term_id); ?>" /></span></a></li>
                        <?php }
                        echo '</ul>';
                    }
                    ?>
                    </li>
                    <?php;
                 }
                echo "</ul> </li>"; // Close the Child UL DIV and parent LI
            }   //endif has subcats ?>
    
        <?php } //end if parent ?>
    <?php } //end foreach ?>
    

Comments are closed.