Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created January 10, 2013 19:12
Show Gist options
  • Select an option

  • Save ocean90/4504872 to your computer and use it in GitHub Desktop.

Select an option

Save ocean90/4504872 to your computer and use it in GitHub Desktop.
Customize the title attributes of term links in the widget tag cloud
<?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