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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制