[Resolved] Filter tag clouds by category

Home Forums Support Filter tag clouds by category

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #860203
    Grant

    Hello,

    Stats show that users of my site use tag clouds. I would therefore like to filter the tag clouds by category and display them above posts in that category, i.e. to create category-specific tag clouds.

    So, for example, this post: https://bit.ly/2YS9uAT is about history and all of the tags above it should only come from the History category. I have tried out one or two plugins that do this filtering, and am using one at present, but they are outdated and don’t filter properly.

    Is it possible to do this using GeneratePress or WP Show Posts?

    Thanks,

    Grant

    GP Premium 1.7.8
    #860626
    David
    Staff
    Customer Support

    Hi there,

    not 100% sure on this but you could try creating a shortcode like this:

    function tag_cloud_shortcode() {
        if(has_tag()) {
            $categories = get_the_category();
            $category_id = $categories[0]->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'   => 10,
            'unit'      => 'px',
            'number'    => 0,
            'format'    => 'flat',
            'order'     => 'count',
            'include'   => $tags_str
            );
            wp_reset_postdata();
            return wp_tag_cloud($args);
        }
    }
    add_shortcode( 'tagscloud', 'tag_cloud_shortcode' );

    Then use the Hook Element to add the [tagscloud] to your posts. Make sure to check Execute shortcode and set your display rules to Posts:

    https://docs.generatepress.com/article/hooks-element-overview/

    #860829
    Grant

    Thanks, David. It works! But makes all of the tags the same size. Is there any way to:

    1. Make the tags attached to more posts display larger, as tag clouds usually do?
    2. Limit the number of tags display, preferably ones that are most used?

    #860851
    David
    Staff
    Customer Support

    So these properties:

    'smallest'  => 10,
    'largest'   => 10,

    Define the font sizes – change the largest to a bigger value and you should see the difference.

    This property sets a limit on the number displayed:

    'number' => 0,

    Set it to 10, and only 10 will be displayed. I think (not sure) if you increase the smallest and largest values then based on the current ordering you should only see the larger end of the scale.

    #863715
    Grant

    Works great! Awesome support, thanks, David!

    #863813
    David
    Staff
    Customer Support

    Awesome – i impressed myself lol.
    glad to be of help

    #863824
    Grant

    Haha! 🙂

    #1219368
    Grant

    Hi again, David,

    I hope all is well with you in these uncertain times. I am just following up on this tag cloud by category issue you helped me with last year. I added the code you gave me (with a few changes to the size and number of the tags, see below) as a code snippet using the Code Snippets plugin:

    function tag_cloud_shortcode() {
        if(has_tag()) {
            $categories = get_the_category();
            $category_id = $categories[0]->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'   => 25,
            'unit'      => 'px',
            'number'    => 30,
            'format'    => 'flat',
            'order'     => 'count',
            'include'   => $tags_str
            );
            return wp_tag_cloud($args);
        }
    }
    add_shortcode( 'tagscloud', 'tag_cloud_shortcode' );

    and it has been working well for almost a year. I just noticed that it is now causing issues with the display of comments, as sometimes the:

    When I deactivate the above code everything works as it should.

    Any ideas on how to resolve this please?

    #1219690
    David
    Staff
    Customer Support

    Hi there,

    before this line: return wp_tag_cloud($args);

    Try adding this on its own line: wp_reset_postdata();

    #1219750
    Grant

    Woah, it works! Thanks so much, David!

    #1220442
    David
    Staff
    Customer Support
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.