[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 - 16 through 30 (of 37 total)
  • Author
    Posts
  • #1500889
    Grant

    Hi David,

    No, it’s not, it’s just generic:

    add_shortcode( 'shortcode_name', function () {
    
    	$out = '<p>write your HTML shortcode content here</p>';
    
    	return $out;
    } );

    whereas yours is specific:

    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'   => 30,
            'unit'      => 'px',
            'number'    => 30,
            'format'    => 'flat',
            'order'     => 'count',
            'include'   => $tags_str
            );
    		wp_reset_postdata();
            return wp_tag_cloud($args);
        }
    }
    add_shortcode( 'tagscloud', 'tag_cloud_shortcode' );

    I have set your code only to run on the front end of the site and with a priority of 10.

    The Hook Element, with the [tagscloud] shortcode is set to show on all Posts and the hook is set to “generate_after_navigation”, execute shortcode is checked and priority is 10.

    #1500963
    David
    Staff
    Customer Support

    Wires crossed there. I am only interested in the code i provided.
    If there is only one Code Snippet containing that code then there should be no conflict.
    It sounds like the code snippet when first enabled has got ‘stuck’ in the system.

    In the code i provided you will see this:

    tag_cloud_shortcode

    There are two instances of this.
    Change them both to something more unique eg.

    grants_tag_cloud_shortcode

    Then try to resave those changes.

    #1510835
    Grant

    Hi David,

    Thanks for your response. I can now activate the snippet without the error message. I still don’t seem to be able to adjust the number of tags that displays. For some reason it is stuck on 30 tags no matter which figure I add to 'number' => XX

    Please let me know if there is anything I can try. Thanks for your patience with this.

    Grant

    #1510950
    David
    Staff
    Customer Support

    Can you share a link to the page where this tag cloud is displayed?

    #1511328
    Grant
    #1512068
    David
    Staff
    Customer Support

    Try changing the Code snippet to this:

    function grants_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'   => 30,
            'unit'      => 'px',
            'number'    => 30,
            'format'    => 'flat',
            'order'     => 'count',
            'include'   => $tags_str
            );
    		wp_reset_postdata();
            return wp_tag_cloud($args);
        }
    }
    add_shortcode( 'tagscloudplus', 'grants_tag_cloud_shortcode' );

    Then in your hook change the shortcode to this:

    [tagscloudplus]

    #1514255
    Grant

    Thanks a lot, David. This does exactly what I want it to. It also does two other things that I am not so keen on:

    Is it easier to address these mobile issues?

    Thank you.

    #1514787
    David
    Staff
    Customer Support

    Did you resolve those issues ? As I am not seeing either of them?

    #1517880
    Grant

    Hi David,

    Seems to be another issue, maybe some leftover AdSense code.

    #1517953
    David
    Staff
    Customer Support

    Could be – if you have Page Caching or a CDN on your server – try clearing them.

    #1520695
    Grant

    Turned out to be an AdSense hook I forgot to unpublish.

    #1520741
    David
    Staff
    Customer Support

    Glad to hear you found the issue!

    #1533170
    Grant

    Hi David,

    Me again. This solution you provided worked out so well that I am wondering if it’s possible to re-use it elsewhere…

    To be clearer, I would like to show tags filtered by a particular category on a page. I have tried just adding the shortcode [tagscloudplus]to a page but I gather that this the shortcode only works if you are viewing a post. Is it easy to tweak to show the tags for a particular category on a page?

    Thanks,

    Grant

    #1533225
    David
    Staff
    Customer Support

    Hi Grant,

    hmmm… so would you be needing to specify which category you want to display in the shortcode eg.

    [tagscloudplus category="something"]

    let us know

    #1533245
    Grant

    Yes, I suppose I would as it wouldn’t be picked up from the post that the user was viewing.

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