我在列出具有唯一类别/分类标题的自定义帖子类型时遇到了一些困难。我有一个ACF反向关系,目前在类别1下有两篇文章,在类别2下有一篇文章。接下来,我试图遍历每个类别并将它们列出如下:
第1类
第2类
但是,以下内容是:
第1类
第1类
第2类
制品
$research = get_posts(array(
'post_type' => 'research-data',
'meta_query' => array(
array(
'key' => 'show_on_page',
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
'compare' => 'LIKE'
)
)
));
?>
<?php if( $research ): ?>
<h3> Research & Data</h3>
<?php foreach( $research as $r ): ?>
<!-- Begin custom tax loop -->
<?php
$categories = get_the_terms($r->ID, 'research-cats', $term_args);
$c_terms = array();
foreach ( $categories as $term ) {
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
//print_r($unique_cat);
?>
<strong><?php echo $unique_cat[0]; ?></strong>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'research-data',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array($r->ID),
'nopaging' => true,
));
foreach($posts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
有什么想法?这让我疯了!
答案 0 :(得分:0)
$research = get_posts(array(
'post_type' => 'research-data',
'meta_query' => array(
array(
'key' => 'show_on_page',
'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123.
'compare' => 'LIKE'
)
)
));
$previous_category = '';
?>
<?php if( $research ): ?>
<h3> Research & Data</h3>
<?php foreach( $research as $r ): ?>
<!-- Begin custom tax loop -->
<?php
$categories = get_the_terms($r->ID, 'research-cats', $term_args);
$c_terms = array();
foreach ( $categories as $term ) {
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
//print_r($unique_cat);
?>
<?php if($previous_category !== $unique_cat[0]) { ?><strong><?php $previous_category = $unique_cat[0]; echo $unique_cat[0]; ?></strong><?php } ?>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'research-data',
'orderby' => 'menu_order',
'order' => 'ASC',
'post__in' => array($r->ID),
'nopaging' => true,
));
foreach($posts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li>
</ul>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
不知道你的数据是如何得到的以及$ research的格式是什么样的,但是我猜测如果你使用上面添加的$ previous_category变量,它应该可以防止这种行为
答案 1 :(得分:0)