【问题标题】:Showing Custom Post Type Taxonomy Terms Next to Post in Wordpress在 Wordpress 中的帖子旁边显示自定义帖子类型分类术语
【发布时间】:2014-12-29 17:16:55
【问题描述】:

我有一个名为 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;?>

【问题讨论】:

  • $query-&gt;post-&gt;ID 还能带回什么东西吗?根据您粘贴的代码,我认为这不是一个有效的变量。无论哪种方式,逐步调试都会非常有用。

标签: php wordpress


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多