The question came up on the Studiopress forum, how to show the Tag Cloud widget as a list in WordPress 4.4 ( showing only the tags of a single post) after previous code stopped working as expected after the update.
Adjusting the output of the tag cloud started breaking the HTML markup, most notably the Widget title is no longer displayed.
This is an example of code that was working before updating to WordPress 4.4
add_filter( 'widget_tag_cloud_args','single_post_tag_cloud_tags' ); /** * Output the tag_cloud only with tags of the single post as a list] * @param array $args Display arguments. * @return array Settings for the output. */ function single_post_tag_cloud_tags($args) { global $post; $post_tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); $args = array( 'include' => implode( ',',$post_tag_ids ), 'largest' => 12, 'smallest' => 12, 'format' => 'list', ); return $args; }
Used in WordPress 4.4 it shows the following:
What happened?
WordPress made a change in the way it outputs the arguments of wp_tag_form in Version 4.4 (see https://core.trac.wordpress.org/changeset/34273).
Behind the scenes the arguments are output before the markup is ready.
We have to somehow improve the timing and “slow down” the execution of our code.
Enter wp_parse_args. We can use this function to merge our changes (custom arguments) with the default values instead of overriding them.
The code needs just a few changes:
add_filter( 'widget_tag_cloud_args', 'single_post_tag_cloud_tags' ); /** * Output the tag_cloud only with tags of the single post as a list] * @param array $args Display arguments. * @return array Settings for the output. */ function single_post_tag_cloud_tags($args) { global $post; $post_tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); $ch_args = array( 'include' => implode( ',',$post_tag_ids ), 'largest' => 12, 'smallest' => 12, 'format' => 'list', ); $args = wp_parse_args( $args, $ch_args ); return $args; }
This is the result on the website
References:
https://core.trac.wordpress.org/changeset/34273
https://wordpress.org/support/topic/wordpress-44-update-broke-tag-cloud#post-7767763
Leave a Reply