查看在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>
可以操纵上面的代码来做我想做的事吗?或者我必须采取一种完全不同的方法。
发布于 2017-03-31 20:13:07
不完全确定这是你想要的,但基本上只需要按类别遍历所有的帖子,然后抓取那些帖子的标签。
您可以尝试这样的方法来获取当前类别的所有标记。您需要对它进行一些操作,以便使用特定的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
?>
https://stackoverflow.com/questions/43148688
复制相似问题