[英]How to display current posts custom taxonomy name inside wordpress loop?
我目前正在建立一个Wordpress网站,但遇到以下困难。
我试图通过显示当前帖子类型的自定义分类名称来动态添加类到HTML元素,以用作类名称。 这些都是在Foreach循环中完成的。
我的代码如下
<?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();?>
这是使用的工作。 ?>显示班级名称,但是在每个班级名称上只显示“兽医”,而在每个项目上都应显示相关部门...
希望有道理。
非常感谢。
对于任何有兴趣的人,我现在都使用以下方法解决了此问题:
<?php $term_list = wp_get_post_terms($post->ID, 'department', array("fields" => "all")); ?>
并通过使用
<?php echo $term_list[0]->slug ; ?>
作为班级名称。
谢谢
您也可以通过此代码解决此问题,将此代码放入while循环中,“ portfolio_category”是自定义分类法名称
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$links = array();
foreach ( $terms as $term ) {
$links[] = $term->name;
}
$tax_links = join( " ", str_replace(' ', '-', $links));
$tax = strtolower($tax_links);
else :
$tax = '';
endif;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.