[Resolved] Filter tag clouds by category

Home Forums Support [Resolved] Filter tag clouds by category

Home Forums Support Filter tag clouds by category

Viewing 7 posts - 31 through 37 (of 37 total)
  • Author
    Posts
  • #1533310
    David
    Staff
    Customer Support

    Try adding this PHP Snippet – it will register a new shortcode function ie. you can still use the old shortcode for posts:

    function category_specific_tag_cloud( $atts ) {
        $atts = shortcode_atts( array(
            'category' => null
        ), $atts );
        // get the category by slug.
        $category = get_category_by_slug( $atts['category'] );
        $category_id = $category->cat_ID;
    
        $query_args = array( 'cat' => $category_id, 'posts_per_page' => -1 );
        $custom_query = new WP_Query( $query_args );
        if ($custom_query->have_posts()) :
            while ($custom_query->have_posts()) : $custom_query->the_post();
                $posttags = get_the_tags();
                if ($posttags) {
                    foreach($posttags as $tag) {
                        $all_tags[] = $tag->term_id;
                    }
                }
            endwhile;
        endif;
    
        $tags_arr = array_unique($all_tags);
        $tags_str = implode(",", $tags_arr);
    
        $args = array(
        'echo'      => false,
        'smallest'  => 10,
        'largest'   => 30,
        'unit'      => 'px',
        'number'    => 30,
        'format'    => 'flat',
        'order'     => 'count',
        'include'   => $tags_str
        );
        wp_reset_postdata();
        return wp_tag_cloud($args);
    }
    add_shortcode( 'tagcloudbycategory', 'category_specific_tag_cloud' );

    You can then add this shortcode to any Page:

    [tagcloudbycategory category="slug"]

    Simply change the slug to that of the category you want displayed.

    #1542142
    Grant

    Works great, thank you!!

    #1542249
    David
    Staff
    Customer Support
    #1567409
    Grant

    Hi David,

    I am still refining this tag cloud. On tablets and cell phones it displays fine but, for some reason, on laptops and computers, the tag cloud displays as full-width – i.e. beyond the parameters of the content blocks. See here for example: https://iafrika.org/izithakazelo-zakwadlamini/. I’m sure it is just a question of padding but please could you explain to me how I ensure that the tag cloud sits in line with the content blocks on all devices?

    Thank you,

    Grant

    #1567666
    David
    Staff
    Customer Support

    Wrap your shortcode in a DIV with the grid-container class like so:

    <div class="grid-container custom-tag-cloud">
    // Shortcode here
    </div>
    #1571443
    Grant

    That’s it! Thanks as always!

    #1571463
    David
    Staff
    Customer Support
Viewing 7 posts - 31 through 37 (of 37 total)
  • You must be logged in to reply to this topic.