Created
January 10, 2013 19:12
-
-
Save ocean90/4504872 to your computer and use it in GitHub Desktop.
Customize the title attributes of term links in the widget tag cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Adds additional args to the widget tag cloud: | |
| * format => array | |
| * topic_count_text_callback => my_remove_default_tag_title | |
| * | |
| */ | |
| function my_add_type_arg( $args ) { | |
| $args['format'] = 'array'; | |
| $args['topic_count_text_callback'] = 'my_remove_default_tag_title'; | |
| return $args; | |
| } | |
| add_filter( 'widget_tag_cloud_args', 'my_add_type_arg' ); | |
| /** | |
| * Helper: Returns an empty string. | |
| */ | |
| function my_remove_default_tag_title() { | |
| return ''; | |
| } | |
| /** | |
| * Generates the new term cloud. | |
| */ | |
| function my_tag_cloud( $tag_output, $tags, $args ) { | |
| // New title string, %s wil be replaced with the term name | |
| $title = 'Alle Beiträge zu %s ansehen'; | |
| $i = 0; | |
| foreach ( $tag_output as $tag ) { | |
| // Check if we have a term id | |
| if ( preg_match( '/-link-(\d+)/', $tag, $match ) ) { | |
| // The term id | |
| $id = $match[1]; | |
| // Get the name of the term | |
| $_title = sprintf( | |
| $title, | |
| get_term_field( | |
| 'name', | |
| $id, | |
| $args['taxonomy'], | |
| 'attribute' | |
| ) | |
| ); | |
| // Replace old title with the new one | |
| $_title = sprintf( 'title="%s"', $_title ); | |
| $tag_output[ $i ] = str_replace( | |
| "title=''", | |
| $_title, | |
| $tag | |
| ); | |
| } | |
| $i++; | |
| } | |
| // Build the term list | |
| $return = join( "\n", $tag_output ); | |
| // Output | |
| echo $return; | |
| } | |
| add_filter( 'wp_generate_tag_cloud', 'my_tag_cloud', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment