在 WordPress 中的帖子旁边显示自定义帖子类型分类术语


Showing Custom Post Type Taxonomy Terms Next to Post in 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;?>

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

<?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>