Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm running a query on a custom post type to display on a page. The custom post type has multiple custom taxonomies. Each taxonomy is not required. I want to display only the taxonomy terms selected, and not the others. How would I achieve this?

$args = array( 'post_type' => 'inspirations', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) :
    $loop->the_post();

    echo '<div class="inspirations-post '. wp_get_post_terms($post->ID, 'collections') . '">';
    the_title();
    echo '<div class="entry-content">';
    the_excerpt();
    echo '</div>';
    echo '</div>';
endwhile;

Right now this returns Array() when printed. I'm guessing I need to break apart the array somehow.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted
$args = array( 'post_type' => 'inspirations', 'posts_per_page' => 10 );  
$loop = new WP_Query( $args );  

while ( $loop->have_posts() ) :
    $loop->the_post();  

    $collections = '';
    foreach ( (array) wp_get_post_terms( get_the_ID(), 'collections') as $collection ) {
        if ( empty($collection->slug ) )
            continue;

        $collections .= ' collection-' . sanitize_html_class($collection->slug, $collection->term_id);
    }

    echo '<div class="inspirations-post '. $collections . '">';
        the_title();  
        echo '<div class="entry-content">';  
            the_excerpt();  
        echo '</div>';  
    echo '</div>';  
endwhile;   
share|improve this answer
    
beautiful, thanks! –  EHerman Dec 10 '13 at 19:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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