3

I've searched far and wide to try and find an answer to my question. I'm hoping I can get help here. Here goes...

I'm currently retrieving the terms of my custom taxonomy using:

<?php echo get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ); ?>

What I'm trying to do is retrieve these same post-specific custom taxonomy terms in a list without them being output as links. I've tried all of the following "solutions," but none of them work. Any help would be appreciated.

Returns the post-specific terms in one long string that can't be put in a list:

$terms_as_text = get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ) ;
echo strip_tags($terms_as_text);

Returns a list of all the terms used across all the custom post types:

<ul>
<?php $args = array( 'taxonomy' => 'skills', 'orderby' => 'ID', 'order' => 'ASC' );
$categories = get_categories( $args );
foreach($categories as $category) { echo '<li> '. $category->name . '</li>'; } 
                ?>
</ul>

Returns nothing:

<?php $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
wp_get_object_terms( $post->ID, $skills, $args );
?>

I've even tried get_the_terms, get_terms, and get_categories to no avail.

10

Can try this:

$terms = get_the_terms ($post->id, 'skills');
if ( !is_wp_error($terms)) : ?>

<?php 
    $skills_links = wp_list_pluck($terms, 'name'); 

    $skills_yo = implode(", ", $skills_links);
    ?>

    <span><?php echo $skills_yo; ?></span>
  • I updated the example on the get_the_terms page. Not sure what that unset is suppose to do for starters... – t31os Jul 21 '11 at 9:59
  • Ya neither did I but was a lazy copy/paste – Wyck Jul 21 '11 at 14:54
  • Thanks both @t31os and Wyck for the help. Here's the code I ended up using which was based on here and the codex. '<?php $terms = get_the_terms( $post->id, 'skills' ); if ( !is_wp_error($terms)) : $skills_links = array(); foreach ($terms as $term) { $skills_links[] = $term->name; $skills = join( "</li><li>", $skills_links ); ?> <ul><li><?php echo $skills; ?></li></ul> <?php endif ?>' – Certified K Jul 22 '11 at 16:53
  • Sorry about the bad post. I can't figure out how to format so it so it's more readable. – Certified K Jul 22 '11 at 16:55
  • i love this answer - clean, usefull code. – Sagive SEO Jan 8 '15 at 13:30
4
$terms = wp_get_post_terms($post->ID, 'TAXONOMYNAME');
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
  • The last one has a comma too... – Sergi Mar 26 at 16:20
1
function term_clean($postid, $term)
{
    $terms = get_the_terms($postid, $term); 
    foreach ($terms as $term) {  echo $term->name;   };

}
1

If you just want the terms assigned to a specific post:

<?php $object_terms = wp_get_object_terms( $post_id, 'skills', array( 'fields' => 'names' ) );
if ( $object_terms ) { ?><ul><li><?php echo implode( '</li><li>', $object_terms ); ?></li></ul><?php } ?>

If you want ALL of the terms:

<?php $all_terms = get_terms( 'skills', array( 'fields' => 'names' ) );
if ( $all_terms ) { ?><ul><li><?php echo implode( '</li><li>', $all_terms ); ?></li></ul><?php } ?>
1

I ran into a similar problem yesterday, and came up with the follow solution:

function taxonomy_list( $taxonomy ) {
    $args = array('order'=>'ASC','hide_empty'=>false);
    $terms = get_terms( $taxonomy, $args );
    if ( $terms ) {
        printf( '<ul name="%s">', esc_attr( $taxonomy ) );
        foreach ( $terms as $term ) {
            printf( '<li>%s</li>', esc_html( $term->name ) );
        }
        print( '</ul>' );
    }
}

Then, just paste <?php taxonomy_list( 'TAXONOMY ID' ); ?> in your template file, replacing TAXONOMY ID with whatever the name of the taxonomy is.

My original usage was to create a list of the job categories I have on my job board. Each one then linked to the taxonomy's archive. You can see the full function in my answer on my own Stackoverflow question.

1

Just use strip_tags

<?php echo strip_tags(get_the_term_list( $post->ID, 'CUSTOM-TAXONOMY', ' ',', ')); ?>
0
// to display taxonomy terms without links: separated with commas
// copy this code in your function.php

function get_taxonony_toDisplay($post_id, $taxonomy_name) {
$terms = wp_get_post_terms($post_id, $taxonomy_name);
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
}

Since I had to display 3 taxonomies separated with commas, so I made a function using Henry's code.

To display use the following line:

<?php get_taxonony_toDisplay($post->ID, 'your_taxonomy_name' ); ?> 
0

If you want the terms ordered by slug rather than name then use this...

<?php if(has_term('', 'CUSTOM-TAX')) {?> 
<?php
    $custom_terms = get_the_terms( get_the_ID(), 'CUSTOM-TAX' );
    // Make sure we have terms and also check for WP_Error object
    if (    $product_terms
    && !is_wp_error( $product_terms )
    ) {
    @usort( $product_terms, function ( $a, $b )
    {
    return strcasecmp( 
    $a->slug,
    $b->slug
    );
    });
    // Display your terms as normal
    $term_list = [];
    foreach ( $custom_terms as $term ) 
    //$term_list[] = esc_html( $term->name );  // comment this line out if you want the terms linked  and visa versa
    $term_list[] = '<a href="' . get_term_link( $term ) . '">' . esc_html( $term->name ) . '</a>'; // comment this line out if you DON'T want the terms linked and visa versa
    echo implode( ', ', $term_list );
    }
?>                          
<?php } else { ?><?php }?>

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.