Created
June 24, 2012 23:23
-
-
Save krogsgard/2985462 to your computer and use it in GitHub Desktop.
shortcode list taxonomy terms
This file contains 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 | |
/** | |
* 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