Wordpress - 仅显示当前类别下的标签

时间:2017-03-31 19:54:45

标签: php wordpress tags

查看在Wordpress中的Archieve.php文件上显示标记,仅显示当前类别下的标记。

目前,我所能做的就是显示所有标签,而不是使用以下代码选择当前类别下的标签:

<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 个答案:

答案 0 :(得分: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
?>
相关问题