Skip to content

Instantly share code, notes, and snippets.

@patric-boehner
Last active August 29, 2015 14:23
Show Gist options
  • Save patric-boehner/d7155f57ddb08f5e4f99 to your computer and use it in GitHub Desktop.
Save patric-boehner/d7155f57ddb08f5e4f99 to your computer and use it in GitHub Desktop.
Tag Cloud Widget Customization

#Customizing WordPress Tag Cloud

This snippet is for creating a function that customizes the parameters of the built in tag cloud in wordpress.

The main change I wanted to make was to adjust the size of the tags to remove any change in size due to popularity of use, potentially also change the order based on name rather then count. I also included snippets for displaying the tagcloud as a list and/or based on an individual taxonomy.

See the comment in the code for more detail.

For additional customizable permeates, see the codex links under References.


#Default Usage

<?php $args = array(
	'smallest'                  => 8, 
	'largest'                   => 22,
	'unit'                      => 'pt', 
	'number'                    => 45,  
	'format'                    => 'flat',
	'separator'                 => "\n",
	'orderby'                   => 'name', 
	'order'                     => 'ASC',
	'exclude'                   => null, 
	'include'                   => null, 
	'topic_count_text_callback' => default_topic_count_text,
	'link'                      => 'view', 
	'taxonomy'                  => 'post_tag', 
	'echo'                      => true,
	'child_of'                  => null, // see Note!
); ?>

The child_of Key is not a direct part of the Array in wp_tag_cloud, but because this function uses wp_parse_args() and get_terms(), you can use all Array Keys used by get_terms()!


#Refrance:
Notes:

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.
//* Customize Tag Cloud Widget
//* https://gist.github.com/patric-boehner/d7155f57ddb08f5e4f99
add_filter('widget_tag_cloud_args','pb_edit_tagcloud_widget');
//* Adjust font size and order by name in ASC order with "/" serperator
function pb_edit_tagcloud_widget($args) {
//* Control Parameters
$args = array(
'separator' => '/'
'orderby' => 'name', //* Order by name or count
'order' => 'ASC', //* Sort the tags in ASCENDING fashion
'unit' => 'px', //* Convert the size to pixels
'smallest' => '16', //* Even list size
'largest' => '16',
);
return $args;
}
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.
//* Customize Tag Cloud Widget
//* https://gist.github.com/patric-boehner/d7155f57ddb08f5e4f99
add_filter('widget_tag_cloud_args','pb_edit_tagcloud_widget');
//* Display as list with corrected font size
function pb_edit_tagcloud_widget($args) {
//* Control Parameters
$args = array(
'format' => 'list',
'orderby' => 'count', //* Order by name or count
'order' => 'ASC', //* Sort the tags in ASCENDING fashion
'unit' => 'px', //* Convert the size to pixels
'smallest' => '16', //* Even list size
'largest' => '16',
);
return $args;
}
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.
//* Customize Tag Cloud Widget
//* https://gist.github.com/patric-boehner/d7155f57ddb08f5e4f99
add_filter('widget_tag_cloud_args','pb_edit_tagcloud_widget');
//* Only display on specific post type archives
//* Display as list with normalized font size
function pb_edit_tagcloud_widget($args) {
if ( ! is_post_type_archive( '' )) //* Limit to Post Type Archive
return;
//* Control Parameters
$args = array(
'format' => 'list',
'unit' => 'px', //* Convert the size to pixels
'smallest' => '16', //* Even list size
'largest' => '16',
'taxonomy' => '', //* Limit to specific custom taxonomy
);
return $args;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment