创建自定义类别导航wordpress

时间:2015-09-15 14:19:25

标签: wordpress navigation categories

我想在wordpress中创建一个自定义类别导航3级。 我不想使用php wp_nav_menu(),因为我需要在导航中添加cateogry缩略图。 我找到了这段代码:

<?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 ?>

但是它在3级上不起作用,仅在2级上。 您可以直接在我的网站上查看菜单:http://www.designsd.fr/ap/

你有想法改变吗?

1 个答案:

答案 0 :(得分:0)

以下是具有新循环级别的更新代码:

<?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 ?>