在循环中显示自定义 post 类型类别(术语)
Display custom post type categories (terms) in loop
我正在使用以下代码从自定义 post 类型加载所有 posts,在这个循环中我显示了一个标题,但我也想显示类别(术语)连接到这个特定的 post 但我似乎无法让它工作
循环:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>"><?php the_title();?></a><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
我试过了:
<?php echo $term->name; ?>
您需要使用 get_the_terms() 来获取类别
你可以通过下面的代码得到这个......
您需要将第二个参数作为自定义 post-type 的类别 slug
你可以参考这个 link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>"><?php the_title();?></a><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
我正在使用以下代码从自定义 post 类型加载所有 posts,在这个循环中我显示了一个标题,但我也想显示类别(术语)连接到这个特定的 post 但我似乎无法让它工作
循环:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>"><?php the_title();?></a><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
我试过了:
<?php echo $term->name; ?>
您需要使用 get_the_terms() 来获取类别 你可以通过下面的代码得到这个...... 您需要将第二个参数作为自定义 post-type 的类别 slug 你可以参考这个 link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>"><?php the_title();?></a><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>