Want to display unlinked terms that the current entry has for a specific custom taxonomy within the loop?
Here’s the sample code for that.
// Displays comma separated Staff Position taxonomy terms. | |
function sk_entry_terms() { | |
$terms = get_the_terms( get_the_ID() , 'staff-position' ); | |
if ( ! empty( $terms ) ) { | |
echo '<p class="entry-meta"><span class="entry-terms">'; | |
foreach ( $terms as $term ) { | |
$entry_terms .= $term->name . ', '; | |
} | |
$entry_terms = rtrim( $entry_terms, ', ' ); | |
echo $entry_terms . '</span></p>'; | |
} | |
} |
Replace staff-position with your custom taxonomy.
and the function call:
<?php echo sk_entry_terms(); ?> |
Slightly different method: http://codex.wordpress.org/Function_Reference/get_the_terms#A_Basic_Example
Thank you. It helped me.
It leaves a comma after the last term. Any way to remove the last comma?
Try replacing
$post->ID
withget_the_ID()
.not sure if my comment was posted so delete this if it’s a duplicate…..
try this instead:
function sk_entry_terms() {
$terms = get_the_terms( get_the_ID() , ‘staff-position’ );
if ( ! empty( $terms ) ) { ?>
name;
}
$output = join( ‘, ‘, $terms_list );
echo $output; ?>
<?php
}
}
that didn’t post correctly it should be this
function sk_entry_terms() {
$terms = get_the_terms( get_the_ID() , ‘staff-position’ );
if ( ! empty( $terms ) ) { ?>
name;
}
$output = join( ‘, ‘, $terms_list );
echo $output; ?>
<?php
}
}
you know what it’s not pasting my code in correctly so just delete my comments
function sk_entry_terms() {
$terms = get_the_terms( get_the_ID() , 'staff-position' );
if ( ! empty( $terms ) ) { ?>
name;
}
$output = join( ', ', $terms_list );
echo $output; ?>
<?php
}
}
Thank you for this one! Helped me out big time with listing job terms from WP Job Manager in a custom sidebar widget.