- This topic has 36 replies, 2 voices, and was last updated 4 years, 1 month ago by David.
-
AuthorPosts
-
April 5, 2019 at 1:18 am #860203Grant
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
April 5, 2019 at 7:29 am #860626DavidStaffCustomer SupportHi 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/
April 5, 2019 at 11:40 am #860829GrantThanks, 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?April 5, 2019 at 12:29 pm #860851DavidStaffCustomer SupportSo 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.
April 9, 2019 at 3:44 am #863715GrantWorks great! Awesome support, thanks, David!
April 9, 2019 at 5:49 am #863813DavidStaffCustomer SupportAwesome – i impressed myself lol.
glad to be of helpApril 9, 2019 at 5:53 am #863824GrantHaha! ๐
March 30, 2020 at 5:33 am #1219368GrantHi 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:
- Wrong comments (i.e. those from another post) display beneath the post, e.g. https://iafrika.org/amaphupho-ngelulwane/
- Comments do not display beneath the post, e.g. https://iafrika.org/ukuhlambulula-idlozi-ngenkolo-yamanazaretha/ (there are 11 comments on this post)
When I deactivate the above code everything works as it should.
Any ideas on how to resolve this please?
March 30, 2020 at 8:02 am #1219690DavidStaffCustomer SupportHi there,
before this line:
return wp_tag_cloud($args);
Try adding this on its own line:
wp_reset_postdata();
March 30, 2020 at 9:08 am #1219750GrantWoah, it works! Thanks so much, David!
March 31, 2020 at 1:34 am #1220442DavidStaffCustomer SupportAwesome ๐
October 22, 2020 at 2:05 pm #1500506GrantHi 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
October 23, 2020 at 1:03 am #1500834DavidStaffCustomer SupportHi Grant
that error occurs if you have registered the same function twice.
Have you created another snippet for a different shortcode ?October 23, 2020 at 1:16 am #1500847GrantHi David,
There is the Example HTML shortcode that comes with Code Snippets. Should I delete that?
October 23, 2020 at 1:46 am #1500877DavidStaffCustomer SupportIs the code in Code Snippets exactly the same as the above code i provided?
-
AuthorPosts
- You must be logged in to reply to this topic.