Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created April 29, 2020 15:05
Show Gist options
  • Save MjHead/decc339f4e0ba82f8a38d3f686a8f34c to your computer and use it in GitHub Desktop.
Save MjHead/decc339f4e0ba82f8a38d3f686a8f34c to your computer and use it in GitHub Desktop.
<?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