简体   繁体   中英

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>';
}

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>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM