dongmiao260399 2017-04-06 22:10
浏览 123
已采纳

在get_posts()中检索post taxonomy

I am retrieving posts from my Custom Post Type using the built-in Wordpress get_posts() function. I am able to print/retrieve all of the data for the post except the post's taxonomy term (which I registered as a custom taxonomy using register_taxonomy( 'developer_category', 'developer', $args ); Here's my code for displaying the posts...

<?php $devs = get_posts([
    'post_type'     => 'developer',
    'posts_per_page'    => '8',
    'orderby' => 'rand'
]); ?>

<div id="loaded-devs">
    <?php $post_count = 1; ?>
    <?php foreach ($devs as $dev): ?>
        <div class="loaded-dev" id="dev<?php echo $post_count; ?>"> 
            <?php echo get_the_post_thumbnail( $dev->ID, 'small' ); ?>
            <a href="<?php echo get_permalink($dev); ?>" class="full-link"></a>
            <h2><?php echo $dev->post_title; ?></h2>
            <p><?php echo get_the_terms($dev->ID); ?> Developer</p>
        </div>
        <?php $post_count ++; ?>
    <?php endforeach; ?>
</div>

<?php wp_reset_postdata(); ?>

Using get_cat_name as I am above returns 'Uncategorized', I believe because it's looking for the default Wordpress category and I am categorizing by custom taxonomy. How can I display my post's taxonomy name inside get_posts() ?

  • 写回答

1条回答 默认 最新

  • dongshanfan1941 2017-04-06 23:33
    关注

    You need to pass the taxonomy to get_the_terms():

    $my_tax_terms = get_the_terms( $dev->ID, 'developer_category' );
    

    This returns an array of terms that you can then loop through. Also, you can't just echo it because it's an array. You can print_r( $my_tax_terms ) to see what you get, but you'll need to loop through the results to pull the information you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建