在帖子上列出自定义分类的所有选定术语

时间:2013-12-10 作者:EHerman

我正在对要显示在页面上的自定义帖子类型运行查询。自定义帖子类型具有多个自定义分类法。不需要每个分类法。我只想显示所选的分类术语,而不想显示其他术语。我将如何实现这一点?

$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;
现在,打印时返回Array()。我想我需要以某种方式分解阵列。

1 个回复
最合适的回答,由SO网友:Shazzad 整理而成
$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;   
结束

相关推荐