0ne year ago i used this code to show most popular tag of the current category in my sidebar:

function my_tags_from_category( $atts, $content = null ) {
extract(shortcode_atts(array(
    'type' => '',
    'count' => 5,
    'verbose' => '0'    // '1' outputs some debug info
), $atts));
$verbose = ( $verbose !== '0' );

$content="";
if ( !empty( $type ) && !empty( $count ) ) {
    $current_cat_ids = array();

    global $post;
    if ( !empty( $post ) ) {
        // get all categories of current post
        $terms = get_the_terms( $post->ID, 'category' );
        if ( $terms && !is_wp_error( $terms ) ) {
            foreach( $terms as $term ) {
                $current_cat_ids[] = $term->term_id;
            }
        }
    } else {
        if ( $verbose ) $content="tags_from_category: no $post...";
    }

    if ( !empty( $current_cat_ids ) ) {
            // get all post ids of current categories ordered by date
            $args = array(
                'posts_per_page' => -1,
                'orderby' => 'post_date',
                'order' => 'DESC',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field' => 'id',
                        'terms' => $current_cat_ids
                    )
                ),
                'suppress_filters' => false,
                'fields' => 'ids'
            );
            $my_post_ids = get_posts( $args );
            if ( !empty( $my_post_ids ) ) {

                switch( $type ) {
                case 'popular-tags':

                    // get terms orderby count
                    // note: wp_get_object_terms for post_id array returns total count of terms on all categories!
                    $found_terms = array();
                    // get terms & count for all post ids of current categories
                    foreach ( $my_post_ids as $my_post_id ) {
                        $terms = get_the_terms( $my_post_id, 'post_tag' );
                        if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
                            foreach ( $terms as $term ) {
                                if ( array_key_exists( 'id_' . $term->term_id, $found_terms ) ) {
                                    $found_terms[ 'id_' . $term->term_id ]['count']++;
                                } else {
                                    $found_terms[ 'id_' . $term->term_id ] = array(
                                        'count' => 1,
                                        'slug' => $term->slug,
                                        'name' => $term->name
                                    );
                                }
                            }
                        }
                    }
                    if ( !empty( $found_terms ) ) {
                        // sort terms by count
                        $found_terms_by_count = array();
                        foreach ( $found_terms as $term_id => $term ) {
                            $found_terms_by_count[ $term_id ] = $term['count'];
                        }
                        array_multisort( $found_terms_by_count, SORT_DESC, $found_terms );
                        // get $count terms order by count
                        // loop through ordered terms until enough tags
                        $content="<ul>";
                        $term_count = 0;
                        foreach ( $found_terms as $term ) {
                            $content .= '<li><a href="' . esc_url( get_term_link( $term['slug'], 'post_tag' ) ) . '" title="' . esc_attr( $term['name'] ) . '">' . $term['name'] . '</a></li>';
                            $term_count++;
                            if ( $term_count >= $count ) {
                                break;
                            }
                        }
                        $content .= '</ul>';
                    } else {
                        if ( $verbose ) $content="tags_from_category: no tags attached to posts in current categories...";
                    }

                    break;
                case 'last-tags':

                    // get $count terms orderby post_date
                    // loop through ordered posts until enough tags
                    $content="";
                    $found_ids = array();
                    foreach ( $my_post_ids as $my_post_id ) {
                        $terms = get_the_terms( $my_post_id, 'post_tag' );
                        if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
                            foreach ( $terms as $term ) {
                                if ( !in_array( $term->term_id, $found_ids ) ) {
                                    $content .= '<li><a href="' . esc_url( get_term_link( $term->slug, 'post_tag' ) ) . '" title="Thema: ' . esc_attr( $term->name ) . '">' . $term->name . '</a></li>';
                                    $found_ids[] = $term->term_id;
                                    if ( count( $found_ids ) >= $count ) {
                                        break 2;
                                    }
                                }
                            }
                        }
                    }
                    if ( !empty( $content ) ) {
                        $content="<ul>" . $content . '</ul>';
                    } else {
                        if ( $verbose ) $content="tags_from_category: no tags attached to posts in current categories...";
                    }

                    break;
                default:
                    if ( $verbose ) $content="tags_from_category: unknown "type"...";
                    break;
                }
            } else {
                if ( $verbose ) $content="tags_from_category: no posts in current categories...";
            }
    } else {
        if ( $verbose ) $content="tags_from_category: no current categories...";
    }
} else {
    if ( $verbose ) $content="tags_from_category: bad params "type" and/or "count"...";
}
return $content;
 }
  add_shortcode( 'tags_from_category', 'my_tags_from_category' );

But the code is not working anymore. i looked several hours for mistakes and triedfew things, but cant find the mistake. Hope anybody can help me..

Best Regards

0

Leave a Reply

Your email address will not be published. Required fields are marked *