php - 与get_the_terms()重复的术语

标签 php wordpress advanced-custom-fields

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

相关文章:

javascript - ACF map - 将标记放在 map 的左侧,而不是中心

wordpress - 高级自定义字段隐藏字段

php - 从现有日期字段构建一个新的 mysql 变量

css - 在 Wordpress (CSS) 中重写 element.style

advanced-custom-fields - 通过 WP API 更新布局内的 ACF 字段

css - 如何解决我的 Wordpress 导航栏的这个问题?

html - 将 Wordpress 小部件 CSS 样式获取到另一个文本 block ?

javascript - 将数据从 MySQL 存储到 PHP 数组并以 JSON 返回

php - 如何动态分配作者在 WordPress 中发帖?

php - 使用 PHP 验证非私有(private) IP 地址