Skip to content

Instantly share code, notes, and snippets.

@krogsgard
Created June 24, 2012 23:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krogsgard/2985462 to your computer and use it in GitHub Desktop.
Save krogsgard/2985462 to your computer and use it in GitHub Desktop.
shortcode list taxonomy terms
<?php
/**
* Displays a list of terms for a specific taxonomy.
* Based on Justin Tadlock's [entry-terms] shortcode
* Added attribute to not link to the taxonomy
* using wp_get_object_terms() to do so
*
* @author Brian Krogsgard
*
* @access public
* @param array $attr
* @return string
*/
function krogs_taxonomy_list_shortcode( $attr ) {
$attr = shortcode_atts( array( 'id' => get_the_ID(), 'taxonomy' => 'post_tag', 'separator' => ', ', 'before' => '', 'after' => '', 'link' => false ), $attr );
$attr['before'] = ( empty( $attr['before'] ) ? '<span class="' . $attr['taxonomy'] . '">' : '<span class="' . $attr['taxonomy'] . '"><span class="before">' . $attr['before'] . '</span>' );
$attr['after'] = ( empty( $attr['after'] ) ? '</span>' : '<span class="after">' . $attr['after'] . '</span></span>' );
$terms = wp_get_object_terms( $attr['id'], $attr['taxonomy']);
$output = '';
if(!empty($terms)){
if(!is_wp_error( $terms )){
$output .= $attr['before'];
$output .= '<ul>';
foreach($terms as $term){
$output .= '<li>';
if ( $attr['link'] ) {
$output .= '<a href="' . get_term_link( $term->slug, $attr['taxonomy'] ) . '">';
}
$output .= $term->name;
if ( $attr['link'] ) {
$output .= '</a>';
}
$output .= $attr['separator'];
$output .= '</li>';
}
$output .= '</ul>';
$output .= $attr['after'];
}
}
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment