2017-03-31 79 views
0

調查顯示Wordpress中Archieve.php文件上的標籤僅顯示當前類別下的標籤。Wordpress - 僅顯示當前類別下的標籤

目前所有的我已經能夠做的是顯示所有的標籤,而不是挑下只是當前的類別與下面的代碼的:

<ul id="blog-tags"> 
<?php 
$tags = get_tags(); 
if ($tags) { 
foreach ($tags as $tag) { 
    echo '<li>'; 

    if ((int) $tag->term_id === get_queried_object_id()) 
     echo "<b>$tag->name</b>"; 
    else 
     printf(
      '<a href="%1$s">%2$s</a>', 
      get_tag_link($tag->term_id), 
      $tag->name 
     ); 

    echo '</li>'; 
} 
} 
?> 
</ul> 

是否有可能操縱我上面的代碼來做我想要的東西?或者我必須採取完全不同的方法。

+1

在這裏你有兩個帳戶嗎?你沒有在這裏發佈這個問題:http://stackoverflow.com/questions/43148089/how-do-you-set-a-active-tag-in-wordpress-that-matches-the-post-your-on ? –

回答

3

不完全確定這是你想要的,但基本上只需要按類別遍歷所有帖子,然後獲取這些帖子的標籤。

你可以嘗試這樣的事情來獲取當前類別的所有標籤。你需要操縱它一下,以某種HTML的方式將其吐出格式。

<?php 
$custom_query = new WP_Query( 
array( 
    'cat' => get_query_var('cat') 
) 
); 
if ($custom_query->have_posts()) : 
while ($custom_query->have_posts()) : $custom_query->the_post(); 
    $posttags = get_the_tags(); 
    if ($posttags) { 
     foreach($posttags as $tag) { 
      $all_tags[] = $tag->term_id; 
     } 
    } 
endwhile; 
endif; 

$tags_arr = array_unique($all_tags); 
$tags_str = implode(",", $tags_arr); 

$args = array(
'smallest' => 12, 
'largest' => 12, 
'unit'  => 'px', 
'number' => 0, 
'format' => 'list', 
'include' => $tags_str 
); 
wp_tag_cloud($args); 
// or use <?php $my_tags = wp_tag_cloud('format=array'); ?> to have them in an array that you can format after 
?> 
+1

我建議在'if'之前聲明'$ all_tags'數組爲'NULL',並在'endif'後面檢查'!is_null($ all_tags)'以防止PHP錯誤。 –

+0

這是一個有效的觀點。 @HarkályGergő –