Rab Austen
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: Change wp_tag_cloud() argument values using hookHi Jeff,
I’ve had the same issue as you and starting from your sample above I then decided to copy entire wp_tag_cloud() function into my themes functions.php and after some minor tweaks it seems to work great.
Here’s my entire function.php entry:
// ---------- Hooking into Tag Cloud STARTS HERE function my_tag_cloud($defaults) { $args = array( 'smallest' => 8, 'largest' => 16, 'unit' => 'pt', 'number' => 25, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true ); $args = wp_parse_args( $args, $defaults ); $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags if ( empty( $tags ) ) return; foreach ( $tags as $key => $tag ) { if ( 'edit' == $args['link'] ) $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] ); else $link = get_term_link( intval($tag->term_id), $args['taxonomy'] ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; $tags[ $key ]->id = $tag->term_id; } $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args $return = apply_filters( 'my_tag_cloud', $return, $args ); if ( 'array' == $args['format'] || empty($args['echo']) ) return $return; echo $return; } add_filter('wp_tag_cloud', 'my_tag_cloud'); // ---------- Hooking into Tag Cloud ENDS HERE
The subtle differences from original function are, swapping the $defaults as the passed in array and $args is my custom array (Although it’s just a copy of the original defaults with some tweaks).
The second important bit to note is the following line:
$return = apply_filters( 'my_tag_cloud', $return, $args );
Which needed to specify ‘my_tag_cloud’ instead of ‘wp_tag_cloud’
Anyway I’m only learning this stuff, so I may have made some huge blunder here but aesthetically it seems to work fine. Hope someone else with more experience can confirm if this is good, bad or indifferent and how I can improve it or perhaps make less bloated.
Hope this helps you too
Cheers
Rab