Created
April 29, 2020 15:05
-
-
Save MjHead/decc339f4e0ba82f8a38d3f686a8f34c to your computer and use it in GitHub Desktop.
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_terms', function( $atts = array() ) { | |
$atts = shortcode_atts( array( | |
'tax' => '', | |
'tax_2' => '', | |
'tax_3' => '', | |
), $atts, 'post_terms' ); | |
$post_id = get_the_ID(); | |
if ( empty( $atts['tax'] ) ) { | |
return; | |
} | |
$result = array(); | |
$terms = wp_get_post_terms( $post_id, $atts['tax'], array( 'orderby' => 'parent' ) ); | |
if ( ! empty( $terms ) ) { | |
foreach ( $terms as $term ) { | |
$result[] = $term->name; | |
} | |
} | |
if ( ! empty( $atts['tax_2'] ) ) { | |
$terms = wp_get_post_terms( $post_id, $atts['tax_2'], array( 'orderby' => 'parent' ) ); | |
if ( ! empty( $terms ) ) { | |
foreach ( $terms as $term ) { | |
$result[] = $term->name; | |
} | |
} | |
} | |
if ( ! empty( $atts['tax_3'] ) ) { | |
$terms = wp_get_post_terms( $post_id, $atts['tax_3'], array( 'orderby' => 'parent' ) ); | |
if ( ! empty( $terms ) ) { | |
foreach ( $terms as $term ) { | |
$result[] = $term->name; | |
} | |
} | |
} | |
return implode( ' ', $result ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment