- This topic has 36 replies, 2 voices, and was last updated 4 years, 2 months ago by
David.
-
AuthorPosts
-
October 23, 2020 at 2:09 am #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.
October 23, 2020 at 3:26 am #1500963David
StaffCustomer SupportWires 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.
October 30, 2020 at 6:15 am #1510835Grant
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
October 30, 2020 at 7:37 am #1510950David
StaffCustomer SupportCan you share a link to the page where this tag cloud is displayed?
October 30, 2020 at 8:45 am #1511328Grant
Sure, here for example: https://iafrika.org/amaphupho-ngokukhuluma/
October 31, 2020 at 4:20 am #1512068David
StaffCustomer SupportTry 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]
November 2, 2020 at 4:23 am #1514255Grant
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:
- On a mobile (the vast majority of my traffic), it puts a white space between the tag cloud and the post. E.g.: https://iafrika.org/izinkolelo-zabantu-ngesimo-sezulu/?highlight=izinkolelo%20zabantu
- Again on a mobile but this time on non-post pages, e.g. on this category page: https://iafrika.org/amasiko/, there is a large grey space where the tag cloud should go.
Is it easier to address these mobile issues?
Thank you.
November 2, 2020 at 8:21 am #1514787David
StaffCustomer SupportDid you resolve those issues ? As I am not seeing either of them?
November 4, 2020 at 8:06 am #1517880Grant
Hi David,
Seems to be another issue, maybe some leftover AdSense code.
November 4, 2020 at 8:47 am #1517953David
StaffCustomer SupportCould be – if you have Page Caching or a CDN on your server – try clearing them.
November 6, 2020 at 7:48 am #1520695Grant
Turned out to be an AdSense hook I forgot to unpublish.
November 6, 2020 at 8:32 am #1520741David
StaffCustomer SupportGlad to hear you found the issue!
November 16, 2020 at 4:23 am #1533170Grant
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
November 16, 2020 at 5:30 am #1533225David
StaffCustomer SupportHi 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
November 16, 2020 at 5:46 am #1533245Grant
Yes, I suppose I would as it wouldn’t be picked up from the post that the user was viewing.
-
AuthorPosts
- You must be logged in to reply to this topic.