检查数组中标签的相等性

Check equals of tags in array

美好的一天! 我有存档页面,在边栏中显示所有 posts 标签。 在存档页面我有循环:

global $b;
        $i=1;
        $b=0;
        while( have_posts() ){
            the_post();
            global $arch_postID;
            global $b;
            if($i == $num_fetch && $sidebar == 'both-sidebar' ) {
            echo '<div class="blog-item' . $item_index . ' gdl-divider ' . $item_class . ' mb30">'; 
            $arch_postID[] = get_the_ID();
            echo $i;
            }elseif($i == $num_fetch) {
            echo '<div class="blog-item' . $item_index . ' gdl-divider ' . $item_class . ' mb20">'; 
            $arch_postID[] = get_the_ID();
            echo $i;
            }else{  
            echo '<div class="blog-item' . $item_index . ' gdl-divider ' . $item_class . ' mb50">';
            $arch_postID[] = get_the_ID();
            echo $i;
            }
            $b++;
            $i++;

在 function.php 侧边栏中,我有:

  global $arch_postID;
    global $b;
    global $archive_uri;    
    if ( !empty($arch_postID) && is_archive() ){         
        echo '<style>.archive-hide-side{display:none;}</style>';
        for($i = 0; $i <= $b-1; $i++){
        $array_keys = array_keys($arch_postID);
        $terms_array = wp_get_post_terms($arch_postID[$i],'vip');  // Get terms for post_id in array( $arch_postID[0], $arch_postID[1] ... )
        $terms_array_next = wp_get_post_terms($arch_postID[$i+1],'vip'); // Get terms for post_id in array( $arch_postID[1], $arch_postID[2] ... )
        $terms_array_last = wp_get_post_terms($arch_postID[$b-1],'vip'); // Get terms for $arch_postID[$i = last]
        $terms_array_first = wp_get_post_terms($arch_postID[0],'vip'); // Get terms for first post ID
        for($a = 0; $a <= count($a)+1; $a++){
        $name_array = $terms_array[$a]->name;
        $name_array_next = $terms_array_next[$a]->name;
        $name_array_last = $terms_array_last[$a]->name;
        $name_array_first= $terms_array_first[$a]->name;
            if( $name_array == $name_array_next or $name_array_next == $name_array_last or $name_array_last == $name_array_first ){ // check if next term->name is similar to previous of last, or first
        $name_array_next = NULL; // assigned NULL to value of term->name
        $name_array_last = NULL; // assigned NULL to value of term->name
        $name_array_first = NULL; // assigned NULL to value of term->name
            echo '<div style="color:black">'. $arch_postID[$i] .' || '. $name_array .'</div>';      // Display term->name values without next or previous term->name
        }
        }   
        }
    
    }

此代码的工作方式类似于 - prntscr.com/7rrozc - 显示所有标签(在右侧)和 post ID(在左侧)除以“||”来自当前存档页面 posts.

最终结果:

我只需要显示一个标签,如果它与另一个相似的话。


我有两个 post,它们的标签:第一个 post( "vip 1", "vip 2", "vip 3", "OTHER" ),第二个 post( "vip 1", "vip 2", "其他 2").


侧边栏必须显示“vip 1”、“vip 2”、“vip 3”、“OTHER”、“OTHER 2”。


我在这个问题上卡了两天,我脑子里没有解决方案...如果有人知道该怎么做,请帮助我。我很乐意提供一点提示。


P.S。感谢您的帮助和时间!

我找到了解决方案,效果很好!

$Path=$_SERVER['REQUEST_URI'];
$Prev_path=$_SERVER['HTTP_REFERER'];
$URI='http://gradrich.tmweb.ru'.$Path;
global $arch_postID;
global $b;
global $archive_uri;
global $brr;
if ( !empty($arch_postID) && is_archive() ){ 
    for($i = 0; $i <= $b-1; $i++){  
    $terms_array = wp_get_post_terms($arch_postID[$i],'super_vip');
    for($a = 0; $a <= 100; $a++){//goes a 100 time from 0, to show all parameters from wp_get_post_terms array
    $terms_name[] = $terms_array[$a]->name;// create array of terms->name
    $terms_slug[] = $terms_array[$a]->slug;// create array of terms->slug
    }           
    }   
    $result_name = array_unique($terms_name);// check name array for unique variables 
    $result_slug = array_unique($terms_slug);// check slug array for unique variables 
    for($f = 0, $s = 0; $f <= count($terms_name), $s <= count($terms_slug); $f++, $s++){// do loop for all unique variables
    if(!empty($result_name[$f]) && !is_category() || !empty($result_slug[$s]) && !is_category()){//check if variables not empty than get proper name and slug from two arrays
    echo '<div class="tagCloud-cover" id="cat-hide"><a href="'. $URI .'+'. $result_slug[$s] .'" >+</a><a href="'. get_post_type_archive_link() .''. $result_slug[$s] .'" rel="tag">' . $result_name[$f] . '</a><a href="'. $Prev_path .'" >-</a></div>';
    } else{
    }
    }
}