[Resolved] Filter tag clouds by category

Home Forums Support [Resolved] Filter tag clouds by category

Home Forums Support Filter tag clouds by category

Viewing 15 posts - 1 through 15 (of 37 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

    #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

    Awesome ๐Ÿ™‚

    #1500506
    Grant

    Hi David,

    I hope all is well. I am once again following up on this code snippet that you gave me a few months back. I have followed your instructions and it seems to be working to some extent. A bit more detail:

    • The snippet remains unactivated. If I try to activate it, I get an error message
    • The Hook Element, with the [tagscloud] shortcode set to show on posts, is published and works. However, when I try and change certain aspects, like the number of tags to display, or the size of the text, nothing changes.

    Any idea of what I might do?
    Thanks,

    Grant

    #1500834
    David
    Staff
    Customer Support

    Hi Grant

    that error occurs if you have registered the same function twice.
    Have you created another snippet for a different shortcode ?

    #1500847
    Grant

    Hi David,

    There is the Example HTML shortcode that comes with Code Snippets. Should I delete that?

    #1500877
    David
    Staff
    Customer Support

    Is the code in Code Snippets exactly the same as the above code i provided?

Viewing 15 posts - 1 through 15 (of 37 total)
  • You must be logged in to reply to this topic.