Skip to content

Instantly share code, notes, and snippets.

@omniacode
Last active June 16, 2017 20:16
Show Gist options
  • Save omniacode/1140842e396c551cb1db88144f9f724d to your computer and use it in GitHub Desktop.
Save omniacode/1140842e396c551cb1db88144f9f724d to your computer and use it in GitHub Desktop.
Custom Taxonomy Shortcode - Wordpress
/* Add Custom Taxonomy Shortcode
/*
* Example: [term-list term="taxonomy term here"]
*/
function custom_taxonomy_list_shortcode( $atts ) {
$atts = shortcode_atts( array(
'term' => 'location',
), $atts );
global $post;
$custom_taxonomy = $atts['term'];
$terms = get_the_terms( $post->ID, $custom_taxonomy );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$output = '<ul class="cpt-term-list">';
foreach( $terms as $term ) {
$output .= '<li>';
$output .= $term->name . '</li>';
}
$output .= '</ul>';
}
return $output;
}
add_shortcode( 'term-list', 'custom_taxonomy_list_shortcode' );
add_filter('widget_text', 'do_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment