Last active
June 16, 2017 20:16
-
-
Save omniacode/1140842e396c551cb1db88144f9f724d to your computer and use it in GitHub Desktop.
Custom Taxonomy Shortcode - Wordpress
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
/* 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