0

I'm looking to display a slug list of the taxonomy term for the current post within the loop.

<?php $terms = get_the_terms( $post->ID, 'wpsc_product_category' ); if ( !empty( $terms ) ){ $term = array_shift( $terms ); echo $term->slug; } ?>

The function I have currently works, although it's only displaying the slug of the first taxonomy term, and not all of them.

e.g. One post is assigned to multiple wpsc_product_category, but only the first term is being display on the front-end, and not a list of all of them.

Any suggestions on how to achieve this would be greatly appreciated.

0

You code is completely wrong. array_shift should not be used

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

You should have a look at the correct use of get_the_terms in the codex.

I would just like to point out, I usually make use of wp_list_categories as it gives a lot of flexibility, and it also works with custom taxonomies. Here is a example from the codex

<?php
$taxonomy = 'wpsc_product_category';

// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
    $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
    $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

    // display post categories
    echo  $terms;
}
?>

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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