如何使用自定义分类检索自定义帖子类型的术语标题?

时间:2017-03-01 作者:Reaper
<?php $the_query = new WP_Query( array(\'post_type\' => \'movie\' ));

if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        ?>

        <div> 
        <?php $term = the_terms($movie->ID,\'Genre\'); ?>

        </div>


    <?php }


    /* Restore original Post Data */
    wp_reset_postdata();
}
else {
    // no posts found
}
?>
1 个回复
SO网友:Milo

它在任何帖子类型和分类的任何循环中都以相同的方式工作,get_the_ID() 检索当前帖子的ID。从the Codex basic example for get_the_terms:

$terms = get_the_terms( get_the_ID(), \'genre\' );
if ( $terms && ! is_wp_error( $terms ) ) { 
    foreach ( $terms as $term ) {
        echo $term->name;
    }
}

相关推荐