douya8978 2016-02-22 12:03
浏览 16
已采纳

如何在wordpress循环中显示当前帖子自定义分类名称?

I am currently building a Wordpress site and I am encountering some difficulty with the following..

I am trying to dynamically add a class to a HTML element by displaying the custom taxonomy name of the current post type, to use as the class name. This is all being done within a Foreach loop.

My code is as follows

<?php
$args = array( 'posts_per_page' => -1,  'post_type' => 'staff', 'orderby' => 'menu_order',
    'order'   => 'DESC');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>

<?php $terms = wp_get_post_terms( $post_ID, 'department' ); ?>
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'department'); ?>

<div class="grid-item  <?php echo $term->slug; ?> ">
<div class="staff-box">
    <?php the_post_thumbnail('staff-member'); ?>
    <a href="<?php echo the_permalink(); ?>">
        <p class="staff-title"><?php the_title(); ?></p>
        <p class="staff-job-title"><?php the_field('staff-job-title'); ?></p>
    </a>
</div>
</div>

<?php endforeach;
wp_reset_postdata();?>

This is working using slug; ?> to display the class name however it is only displaying "veterinary-surgeons" on every single class name, when it should be displaying the relevant department on each item...

Hope that makes sense.

Many thanks.

</div>
  • 写回答

2条回答 默认 最新

  • doujia9204 2016-02-22 12:50
    关注

    To anyone who is interested I have now solved this using:

    <?php $term_list = wp_get_post_terms($post->ID, 'department', array("fields" => "all")); ?>

    and by using

    <?php echo $term_list[0]->slug ;  ?>

    as class name.

    Thanks

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀