Search code examples
phpwordpresscustom-post-typecustom-taxonomy

Getting custom taxonomy tags based on get_the_ID() (Outside for loop)


I have a custom taxonomy called type. Type has the following options:

  • Blog
  • News
  • Training

I've created a draggable element (in Visual Composer) which will show these tags. Because of this, the draggable element is not part of archive-resources.php, so I can't run it through a loop.

What I'm trying to do is:

  • Get blog post ID (which I've done via get_the_ID().
  • Display type that is assigned that blog post.

However, currently, all three type tags are displaying. Where am I going wrong?

$blogpostID = get_the_ID();
$termType = get_terms('type');
$output = '';

foreach ( $termType as $termT ) {
    echo $output . '<a href="'.get_term_link($termT).'">'.$termT->name.'</a>';
}

Solution

  • Ok, Do it this way :

    <?php  
    $terms = get_the_terms( $post->ID , 'type' );
    $output = '';
    if ( $terms != null ){
        foreach ( $terms as $term ) {
            echo $output . '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
        }
    }