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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀