Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Created November 5, 2012 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enigmaticape/4017131 to your computer and use it in GitHub Desktop.
Save enigmaticape/4017131 to your computer and use it in GitHub Desktop.
wp_list_tags a-like, mimics (some of) the functionality of wp_list_categories
function theme_list_tags( $args ) {
$current_url = $_SERVER['REQUEST_URI'];
/* get_tags shares a subset of its parameters with
wp_get_catgeories, :
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true
'exclude' => ''
'include' => ''
'number' => null
hierarchical => true
So we'll just pass the $args through.
See the codex for further details of additional
get_tags() parameters
*/
$tags = get_tags( $args );
$html = '<div class="taglist">';
foreach ($tags as $tag){
$tag_link = get_tag_link($tag->term_id);
$html .= "<li class='tag_item {$tag->slug}";
if( substr($tag_link, -strlen($current_url) ) === $current_url ) {
$html.=" current-tag";
}
$html .= "'><a href='{$tag_link}'>";
$html .= "{$tag->name}";
if( $args['show_count'] == true) {
$html .= " (".$tag->count.")";
}
$html .= "</a></li>";
}
$html .= '</div>';
echo $html;
}
@enigmaticape
Copy link
Author

This code adds a 'current_tag' class to any tag whose URL can be found at the end of tyhe current Wordpress URL in the same way as the list_categories function does. Blogged at http://www.enigmaticape.com/blog/wordpress-listing-tags-like-wp_list_categories/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment