0

Ok, I spent a lot of time trying to figure this out and hours on Google, but I just can't seem to get this to be dynamic. I have a shortcode that spits out an unordered list of taxnonomy terms associated with my Custom Post Type, but currently I have to copy & paste for each taxonomy term I want to show. I am trying to figure out how to make it dynamic in the way I can just use the same shortcode and just "pass in" a different taxonomy term.

function list_bar_location_taxonomy( $atts ) {

  global $post;

  $taxonomy = 'location';
  $locations = get_the_terms( $post->ID, $taxonomy );


  if ( ! empty( $locations ) && ! is_wp_error( $locations ) ) {
    $output = '<ul class="location-meta">';

    foreach( $locations as $location ) {
        $output .= '<li>';
        $output .= $location->name . '</li>';
    }

    $output .= '</ul>';
  }

  return $output;
}

add_shortcode( 'bars_location', 'list_bar_location_taxonomy' );

I want the taxonomy term 'location' to be interchangeable so I can pass in whatever taxonomy to be used and displayed on the front-end. e.g. taxterm2, taxterm3, etc.

I tried the following to no avail:

function list_bar_location_taxonomy( $atts ) {
  $atts = shortcode_atts( 
    array( 
      'custom_taxonomy' => ' '
    ), $atts
  );

  global $post;

  $locations = get_the_terms( $post->ID, $atts[ 'custom_taxonomy' ] );


  if ( ! empty( $locations ) && ! is_wp_error( $locations ) ) {
    $output = '<ul class="location-meta">';

    foreach( $locations as $location ) {
        $output .= '<li>';
        $output .= $location->name . '</li>';
    }

    $output .= '</ul>';
  }

  return $output;
}

add_shortcode( 'bars_location', 'list_bar_location_taxonomy' );

Any insight as to what I am doing wrong would be greatly appreciated. Thanks.

2
  • I just tried your second code, and it is working fine for me. For example I created a custom-post-type with the slug customtype, I created a custom-taxonomy for this post-type with the slug customtax, I added multiple post to this type and added some terms, custom-term-1, custom-term-2, custom-term-3. I inserted the shortcode [bars_location custom_taxonomy="customtax"] into one of these custom posts. On the frontend I can now see an unordert list with the 3 terms associated with that post, e.x. Custom Term 1, Custom Term 2, Custom Term 3. What is not working for you? – LWS-Mo Jun 14 '17 at 8:39
  • The shortcode will work without the [custom_taxonomy="customtax"] appended to it. All is needed is [bars_location] with either block of code. The end goal is to be able to switch out "customtax" for other taxonomy terms using the same shortcode. e.g. [bars_shortcode custom_taxonomy="customtax-location"] [bars_shortcode custom_taxonomy="customtax-types-of-bars"] [bars_location custom_taxonomy="customtax-someothertaxonomy"] – Jason Ryan Jun 15 '17 at 12:50
0

Ok, I figured out the solution and have it working to where I can swap out the attribute for the shortcode with any taxonomy available associated with every post type:

function custom_taxonomy_list_shortcode( $atts ) {
  $atts = shortcode_atts( array(
    'term' => 'location', //Set whatever default taxonomy you like
  ), $atts );

  global $post;

  $custom_taxonomy = $atts['term']; // Assign it to a var
  $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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.