0

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.

0
$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;   

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.