【发布时间】:2015-07-12 13:25:03
【问题描述】:
美好的一天! 我有存档页面,在侧边栏中显示所有帖子标签。 在存档页面我有循环:
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 - 显示所有标签(右侧)和帖子 ID(左侧)除以“||”来自当前存档页面的帖子。
最终结果:
我只需要显示一个标签,如果它与另一个相似。
我有两个帖子,它们的标签:第一个帖子(“vip 1”,“vip 2”,“vip 3”,“OTHER”),第二个帖子(“vip 1”,“vip 2”,“OTHER 2” ")。
在侧边栏中必须显示“vip 1”、“vip 2”、“vip 3”、“OTHER”、“OTHER 2”。
我在这个问题上停留了两天,我没有解决方案......如果有人知道如何做到这一点,请帮助我。我很乐意提供一点提示。
附:感谢您的帮助和时间!
【问题讨论】:
-
[offtopic] 如果您使用非说变量,代码可能会变得非常不清楚。 :-)
-
对不起,我发布了测试版本的代码。更改一些代码以便更好地理解。谢谢!
标签: php wordpress loops tags compare