1

I created this shortcode to display my custom taxonomy terms:

add_shortcode( 'taxonomy_lists', 'wptls_taxonomy_lists' );

function wptls_taxonomy_lists( $atts ) {
    $atts = shortcode_atts( array(
        'name' => 'category',
        'hide_empty' => false,
        'exclude' => '',
        'include' => '',
        'order' => 'ASC',
        'orderby' => 'name',
        'search_bar' => 0,
        'show_count' => false,
        'count_type' => 'terms'     
    ), $atts);


    $terms = get_terms( array(
        'taxonomy' => $atts['name'],
        'hide_empty' => filter_var( $atts['hide_empty'], FILTER_VALIDATE_BOOLEAN),
        'orderby' => $atts['orderby'],
        'order' => $atts['order'],
        'parent'   => 0
    ) );

    $html = '';
    if( $atts['search_bar'] == 1 )
        $html .= wptls_search_filter();

    $html .= '<div class="taxonomy-lists" id="';
    
    $the_page = sanitize_post( $GLOBALS['wp_the_query']->get_queried_object() );
    $slug = $the_page->post_name;
    $html .= $slug;
    $html .= '">';

    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        foreach ( $terms as $term ) {
            
            if( !empty( $atts['exclude'] ) ) {
                $exclude = explode( ',',$atts['exclude'] );
                if( in_array( $term->term_id, $exclude) ) {
                    continue;
                }
            }
            
            if( !empty( $atts['include'] ) ) {
                $include = explode( ',',$atts['include'] );
                if( !in_array( $term->term_id, $include) ) {
                    continue;
                }
            }
            
            $image = '';
            if( function_exists('get_woocommerce_term_meta') ) {
                $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
                $image = wp_get_attachment_url( $thumbnail_id );
            }
                
            $html .= '<div class="taxonomy-lists-item" data-taxname="'.strtolower( $term->name ).'">';
            $term_link = get_term_link( $term );
            
            if( wptls_has_child_terms( $atts['name'],$term->term_id,$atts['hide_empty'] ) == true )
                $html .= '<div class="tax-arrow">►</div>';
            
            $child_terms = get_terms( array(
                'taxonomy' => $atts['name'],
                'hide_empty' => filter_var( $atts['hide_empty'], FILTER_VALIDATE_BOOLEAN),
                'orderby' => $atts['orderby'],
                'order' => $atts['order'],
                'parent'   => $term->term_id
            ) );

            $html .= '<div class="tax-details">
                <div class="tax-name">                
                    <div class="tax-image">
                        <img src="'.$image.'" width="50">
                    </div>
                    
                    <div class="tax-title">
                        <a href="' . esc_url( $term_link ) . '" title="'.$term->description.'">' . $term->name . '</a>
                    </div>';

            if( $atts['count_type'] == 'post' && filter_var( $atts['show_count'], FILTER_VALIDATE_BOOLEAN) == true ) {
                $html .= '<div class="tax-child-count">('. get_posts_count( $term ) .')</div>';
            }
                    
            if( $atts['count_type'] == 'terms' && ( is_array( $child_terms ) && count( $child_terms ) > 0 && filter_var( $atts['show_count'], FILTER_VALIDATE_BOOLEAN) == true ) ) {
                $html .= '<div class="tax-child-count">('. count( $child_terms ) .')</div>';
                
            }

            $html .= '</div>';
            $html .= '<div class="tax-desc">' . $term->description . '</div>
                </div>';            
            
            if( !empty( $child_terms ) && is_array( $child_terms ) ) {
                foreach ( $child_terms as $child_term ) {
                    
                    $image = '';
                    if( function_exists('get_woocommerce_term_meta') ) {
                        $thumbnail_id = get_woocommerce_term_meta( $child_term->term_id, 'thumbnail_id', true );
                        $image = wp_get_attachment_url( $thumbnail_id );
                    }
                    
                    $html .= '<div class="tax-child-list-item">';
                    $child_term_link = get_term_link( $child_term );
                    $html .= '<div class="tax-name">
                            <div class="tax-image"><img src="'.$image.'" width="50"></div><div class="tax-title"><a href="' . esc_url( $child_term_link ) . '" title="'.$child_term->description.'">' . $child_term->name . '</a></div>';
                    
                    
                    if( $atts['count_type'] == 'post' && filter_var( $atts['show_count'], FILTER_VALIDATE_BOOLEAN) == true ) {
                        $html .= '<div class="tax-child-count">('. get_posts_count( $child_term ) .')</div>';                        
                    }

                    $html .= '</div>';

                    $html .= '<div class="tax-desc">' . $child_term->description . '</div>';
                    $html .= '</div>';
                }
            }
            
            $html .= '</div>';
        }
    }
    
    $html .= '</div>';
    return $html;
}

I tried to order terms by name, but it doesn't work. I checked with other post about same issue, but I can't find a solution. Can anyone help me?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.