gpt4 book ai didi

php - 与get_the_terms()重复的术语

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:52 24 4
gpt4 key购买 nike

我很难列出具有唯一类别/分类标题的自定义文章类型。我有一个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 &amp; 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; ?>

有什么想法吗?这让我发疯了!

最佳答案

$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 &amp; 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变量,应该可以防止这种行为

关于php - 与get_the_terms()重复的术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com