[英]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.