jlittlejohn
9/10/2014 - 4:31 PM

WP: Build Custom Taxonomy Navigation

WP: Build Custom Taxonomy Navigation

function build_category_navigation( $cat = null ){

        $args = array(
            'hierarchical' => 1,
            'pad_counts' => 1,
            'taxonomy' => 'categories',
            'order_by' => 'id',
            'order' => 'asc'
        );

        // work around for parent/pad_counts bug

        $categories = get_categories( $args );
        $categories = wp_list_filter($categories,array('parent' => (($cat != null) ? $cat : 0)));

        if (count($categories) > 0){

            foreach ($categories as $category){

                echo '<li class="category-li ' . $category->slug . '">';

                if (!isset($cat)) {
                  echo '<a class="category" href="' . get_category_link( $category->term_id ) . '">'
                        . '<i class="fa fa-caret-right"></i> ' . $category->name
                        . ' (' . $category->count . ')</a>';
                } else {

                    echo '<a class="sub-category" href="' . get_category_link( $category->term_id ) . '">'
                        . '<i class="fa fa-caret-right"></i> ' . $category->name
                        . ' (' . $category->count . ')</a>';

                }

                // query posts in current category

                $query = new WP_Query( array('post_type' => array('article'), 'tax_query' => array(
                    array(
                        'taxonomy' => 'categories',
                        'field' => 'id',
                        'include_children' => false,
                        'terms' => $category->cat_ID //array()
                    )
                )));
                $hasSubCategories = false;
                
                if(count(get_categories(array('parent' => $category->ID))) > 0){
                    $hasSubCategories = true;
                }

                if($query->have_posts() || $hasSubCategories){

                    echo '<ul class="children">';

                    while($query->have_posts()){

                        $query->the_post();
                        echo '<li><a class="article" href="' . get_permalink() . '">'
                            . '<i class="fa fa-file-o"></i> ' . get_the_title() . '</a></li>';

                    }

                    // recursively build other subcategories

                    build_category_navigation($category->cat_ID);

                    echo '</ul>';

                }

                echo '</li>';

            }

        } else {

            return false;

        }

        // return;

        wp_reset_postdata();

    }



    function get_the_category_bytax( $id = false, $tcat = 'category' ) {
    $categories = get_the_terms( $id, $tcat );

    if ( ! $categories )
        $categories = array();

    $categories = array_values( $categories );

    foreach ( array_keys( $categories ) as $key ) {
        _make_cat_compat( $categories[$key] );
    }

    // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
    return apply_filters( 'get_the_categories', $categories );
}