Created
March 15, 2017 22:20
-
-
Save khromov/a9fb541fd8e003a1f20dbdbf3765ad73 to your computer and use it in GitHub Desktop.
Print taxonomy terms starting from specific slug in WordPress
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 | |
| /* | |
| * Plugin Name: Print taxonomy terms starting from specific slug | |
| * Description: Usage: [print_taxonomy slug="product-category"] or <?php echo do_shortcode('[print_taxonomy slug="product-category"]'); ?> | |
| */ | |
| add_shortcode('print_taxonomy', function($atts, $content) { | |
| $atts = shortcode_atts( array( | |
| 'slug' => '', | |
| ), $atts, 'print_taxonomy' ); | |
| $taxName = 'category'; | |
| $terms = []; | |
| $terms[] = get_term_by('slug', $atts['slug'], $taxName); | |
| foreach($terms as $term) { | |
| echo '<a href="' . get_term_link( $term->slug, $taxName ) . '">' . $term->name . '</a>'; | |
| $term_children = get_term_children( $term->term_id, $taxName ); | |
| echo '<ul>'; | |
| foreach ( $term_children as $term_child_id ) { | |
| $term_child = get_term_by( 'id', $term_child_id, $taxName ); | |
| echo '<li><a href="' . get_term_link( $term_child->name, $taxName ) . '">' . $term_child->name . '</a></li>'; | |
| } | |
| echo '</ul>'; | |
| } | |
| return ob_get_clean(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment