在Wordpress中显示Post后面的自定义帖子类型分类术语

时间:2014-12-29 17:16:55

标签: php wordpress

我有一个名为recipe的自定义帖子类型以及名为cuisinerecipe_type的自定义分类。

我想在页面上显示10个食谱的列表以及它所属的分类法的相关术语:

我有以下代码,其中显示了食谱,但没有显示分类术语:

<?php 
query_posts(array( 
    'post_type' => 'recipe',

    'showposts' => 10
) );  
?>
<?php while (have_posts()) : the_post(); ?>
    <li>
    <?php the_title(); ?> 
    <?php $terms = wp_get_post_terms( $query->post->ID, array( 'cuisine', 'recipe_type' ) ); ?>

    <?php foreach ( $terms as $term ) : ?>
    <p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
    <?php endforeach; ?>

    </li>


<?php endwhile;?>

1 个答案:

答案 0 :(得分:0)

以下代码用于显示与自定义帖子类型相关的分类术语

<?php 
query_posts(array( 
'post_type' => 'recipe',

'showposts' => 10
) );  
?>

<?php while (have_posts()) : the_post(); ?>
<li>
<?php the_title(); ?> 
<?php $terms = get_the_terms( $post->ID , 'recipe_type' ); ?>

<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>

</li>