如何从wp_get_post_terms()中删除重复项?

时间:2019-10-17 06:05:14

标签: php wordpress

我试图根据以下代码中的term_ids遍历帖子,得到了包含一个重复项的以下数组:

$terms = wp_get_post_terms($post->ID,'book',array("fields" => "all"));
print_r($terms);
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 10
            [name] => Life Style
            [slug] => life-style
            [term_group] => 0
            [term_taxonomy_id] => 10
            [taxonomy] => book
            [description] => 
            [parent] => 0
            [count] => 1
            [filter] => raw
        )

    )

Array
(
    [0] => WP_Term Object
        (
            [term_id] => 7
            [name] => My First Novel
            [slug] => my-first-novel
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => book
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

    )
Array
(
    [0] => WP_Term Object
        (
            [term_id] => 7
            [name] => My First Novel
            [slug] => my-first-novel
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => book
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

    )

我尝试了array_unique函数,但是没有用

Category.php页面

while(have_posts()) : the_post();

$terms =  wp_get_post_terms($post->ID,'book',array("fields" => "all"));                        
if($terms[0]->taxonomy !== 'book'){show posts}
if($terms[0]->taxonomy === 'book' && isset($terms[0]->term_id)){
    foreach ($terms as $term):

        $args = array(

            'post_type' => 'post',
            'order' => 'ASC',
            'showposts' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => $term->taxonomy,
                    'field' => 'term_id',
                    'terms' => $term->term_id
                )
             ),

         );
         $the_query = new WP_Query( $args );

         while($the_query->have_posts(): $the_query->the_post:
             show posts
         endwhile;

    endforeach;
} 

所以我想做的是:我有自定义分类法“ book”和默认分类法类别作为图书类型,因此,如果有人想明智地发表整篇小说,那么他将在“ book”分类法下创建图书标题并创建帖子作为章节并选择书籍类型说小说,当任何人点击小说类别作为类别时,他都会看到书名和书籍封面图片,而作为小说章节的所有帖子都会显示在引导弹出窗口上。

因此,该类型可能包含一整本书,也可能仅包含一则短篇小说或诗歌,单个帖子将按原样显示,而整本书将显示为包含章节的书。

请提供帮助和建议。

谢谢。

0 个答案:

没有答案