所以我有一个自定义的帖子类型Video
还有一种分类法叫做category
我用来将视频分组,所以我想这样列出它们:
Taxonomy NAME 1
VIDEO 1
VODEO 2
Taxonomy NAME 2
Video 1
Video 2
所以我尝试了一些方法,但没有成功,问题是对于每个视频,我也会显示分类名称,如下所示:
Taxonomy Name 1
Video 1
Taxonomy Name 1
Video 2
所以这不是我想要的,也许你们能弄明白,我做错了什么:
<?php get_header();?>
<?php
$args = array( \'post_type\' => \'Video\');
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $fg = get_the_terms(get_the_ID(),\'category\'); ?>
<?php foreach($fg as $term): ?>
<h2><?php echo $term->name; ?></h2>
<?php wp_reset_postdata(); ?>
<?php
$posts = new WP_Query(array(\'post_type\' => \'Video\', \'category\' => $term->name) );
while ( $posts->have_posts() ) : $posts->the_post();
the_post_thumbnail();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
?>
<?php endforeach; ?>
<?php endwhile; ?>
<?php get_footer(); ?>
如果有人能帮忙,请帮我:(我弄不到这个
最合适的回答,由SO网友:Ryan McCue 整理而成
根据阿南德的评论,这样的做法应该会奏效。
<?php get_header();?>
<?php
$args = array( \'post_type\' => \'Video\');
$loop = new WP_Query( $args );
$old_term = null;
while ( $loop->have_posts() ):
$loop->the_post();
$fg = get_the_terms(get_the_ID(),\'category\');
foreach($fg as $term):
if ( $old_term === null || $old_term != $term ):
$old_term = $term;
?>
<h2><?php echo $term->name; ?></h2>
<?php
endif;
$posts = new WP_Query(array(\'post_type\' => \'Video\', \'category\' => $term->name) );
while ( $posts->have_posts() ) : $posts->the_post();
the_post_thumbnail();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php
endwhile;
wp_reset_postdata();
endforeach;
endwhile;
?>
<?php get_footer(); ?>