- This topic has 9 replies, 3 voices, and was last updated 3 years, 2 months ago by
David.
-
AuthorPosts
-
January 29, 2023 at 12:51 pm #2513335
finanzbeben
Hi there.
First of all, I use a code snippet to enable tags not only for posts but also for pages.
// add tag support to pages function tags_support_all() { register_taxonomy_for_object_type('post_tag', 'page'); } // ensure all tags are included in queries function tags_support_query($wp_query) { if ($wp_query->get('tag')) $wp_query->set('post_type', 'any'); } // tag hooks add_action('init', 'tags_support_all'); add_action('pre_get_posts', 'tags_support_query');Having achieved this, I want to use the standard tag cloud widget to present tags used on the respective page/post only. So I don’t want to see all tags for the whole website. Further, I want the tags to be shown in descending order by frequency.
I was not able to find any code so far helping me.
Do you have any idea how to manipulate the standard tag cloud to achiev this? Or any other workaround?
Kind regards
January 29, 2023 at 7:49 pm #2513579Fernando Customer Support
Hello there,
Instead of using a Tag Cloud Block, I would recommend using a Block Element – Hook and adding a Headline Block. Through a Headline Block, you can retrieve Tags for specific posts with such settings: https://share.getcloudapp.com/xQuOkxAm
Can you try this?
January 30, 2023 at 2:08 am #2513789finanzbeben
Helle Fernando,
Thanks a lot. I have slightly adjusted your approach: I am using a button to show the list of terms.
However, is there any possibility to sort the dynamic text type (list of terms) in descending order by frequency?Kind regards
January 30, 2023 at 4:40 am #2513961David
StaffCustomer SupportHi there,
its not possible with the block dynamic data at this time.
And the core Tag Cloud Block, has no filters either:https://github.com/WordPress/gutenberg/issues/33990
unlike the old widget you can’t change its behaviour.
Instead you can create your own tag cloud shortcode by adding this PHP Snippet:
function tag_cloud_shortcode() { if( has_tag() && is_singular() ) { global $post; $post_tags = get_the_terms($post->ID,'post_tag'); if ($post_tags) { foreach($post_tags as $tag) { $all_tags[] = $tag->term_id; } } $tags_arr = array_unique($all_tags); $tags_str = implode(",", $tags_arr); $args = array( 'echo' => false, 'smallest' => 10, 'largest' => 20, '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 you can use the shortcode:
[tagscloud]January 30, 2023 at 4:53 am #2513971finanzbeben
Hi David, Thanks for this workaround option. It works only for posts, not for pages. Do you know how to tweak your snippet to also address pages? Otherwise, I think I will simply stick to the button option for the time being. Kind regards
January 30, 2023 at 6:08 am #2514037David
StaffCustomer SupportAh, i edited the code above to make it apply to any post type.
January 30, 2023 at 6:24 am #2514052finanzbeben
Now, it works pretty decent. Thank you.
In terms of styling, do you know if it is also possible to shift from the “standard” to “outline” output in this custom tag cloud? The outline output looks like buttons whereas the standard shows the tags in different sizes depending on the frequency.
January 30, 2023 at 7:43 am #2514140David
StaffCustomer SupportOk
in the code above you will see these two args:'smallest' => 10, 'largest' => 20,Those set the font size range, if you set them both to the same value eg. 14 then all tags will be the same size of 14px/
Then add some CSS to style like a button:
.tag-cloud-link { border: 1px solid; border-radius: 50px; padding: 6px; background-color: #f00; color: #fff; }January 30, 2023 at 8:06 am #2514300finanzbeben
Thank you so much!
January 31, 2023 at 4:12 am #2515337David
StaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.