Last active
August 29, 2015 14:01
-
-
Save jacobwise/1ea35ed24556d39511e5 to your computer and use it in GitHub Desktop.
Creates a shortcode called 'post_custom_tax' that accepts a custom taxonomy as a variable
This file contains hidden or 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 | |
| add_shortcode( 'post_custom_tax', 'jw_post_custom_taxonomy_shortcode' ); | |
| /** | |
| * Produces the custom taxonomy terms links list. | |
| * | |
| * Supported shortcode attributes are: | |
| * taxonomy (taxonomy to list, default is category), | |
| * after (output after link, default is empty string), | |
| * before (output before links, default is 'Filed Under: '), | |
| * sep (separator string between tags, default is ', '). | |
| * | |
| * Output passes through 'jw_post_custom_taxonomy_shortcode' filter before returning. | |
| * | |
| * | |
| * @param array|string $atts Shortcode attributes. | |
| * @return string Shortcode output | |
| */ | |
| function jw_post_custom_taxonomy_shortcode( $atts ) { | |
| $defaults = array( | |
| 'taxonomy' => 'category', | |
| 'sep' => ', ', | |
| 'before' => __( 'Filed Under: ', 'swellfire' ), | |
| 'after' => '' | |
| ); | |
| $atts = shortcode_atts( $defaults, $atts, 'post_custom_tax' ); | |
| $terms = wp_get_post_terms( get_the_ID(), $atts[ 'taxonomy' ] ); | |
| if ( !empty( $terms ) && !is_wp_error( $terms ) ){ | |
| $count = count( $terms ); | |
| $i = 0; | |
| $post_meta .= '<span class="categories">'; | |
| $post_meta .= $atts['before']; | |
| foreach ( $terms as $term ) { | |
| $i++; | |
| $term_link = get_term_link( $term, $atts[ 'taxonomy' ] ); | |
| $post_meta.= '<a href="' . $term_link . '" >' . $term->name . '</a>' ; | |
| if ($count != $i) { | |
| $post_meta .= $atts['sep']; | |
| } | |
| } | |
| $post_meta .= '</span>'; | |
| } | |
| return apply_filters( 'jw_post_custom_taxonomy_shortcode', $post_meta, $atts ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment