WordPress.org

Ready to get started?Download WordPress

Forums

Displaying a hierarchical, linked list of category/taxonomy terms in WordPress (1 post)

  1. nerdland
    Member
    Posted 1 year ago #

    I'm having a small issue getting this list of subcategories to display the way I want them to. (Actually, it's a custom taxonomy in this case, but it's the same idea.) I have the following code, and it ALMOST works perfectly. I want the parent taxonomy term NOT to be linked but all of the child taxonomy terms to be linked.

    For some reason, when I use get_category_link( $cat->term_id ), it doesn't return anything at all in the link.

    <?php
    foreach( get_categories('taxonomy=location&hide_empty=false') as $cat ) :
    if( !$cat->parent ) {
        echo '<ul><li><strong>' . $cat->name . '</strong></li>';
        process_cat_tree( $cat->term_id );
    }
    endforeach;
    
    wp_reset_query();
    
    function process_cat_tree( $cat ) {
        $args = array('category__in' => array( $cat ), 'numberposts' => -1);
        $cat_posts = get_posts( $args );
        $next = get_categories('taxonomy=location&hide_empty=false&parent=' . $cat);
    
        if( $next ) :
            foreach( $next as $cat ) :
                echo '<ul><li><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></li>';
                process_cat_tree( $cat->term_id );
            endforeach;
        endif;
    
        echo '</ul>';
    }
    ?>

    I'm sure it's my own stupidity, but can anyone enlighten me?

Topic Closed

This topic has been closed to new replies.

About this Topic