【发布时间】:2011-03-16 05:20:13
【问题描述】:
这个功能是否内置在 wordpress 中?我没有在法典中看到任何内容。
codex.wordpress.org/Function_Reference/wp_tag_cloud
我有几个特定类别的页面,我想显示与这些帖子相关的所有标签。
我确实找到了这个,但我不确定它是否正确或是否存在更好的方法(source)(旧方法!!!):
<?php
query_posts('category_name=html');
if (have_posts()) : while (have_posts()) : the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags_arr[] = $tag -> name;
}
}
endwhile; endif;
$tags_arr = array_unique($all_tags_arr);
?>
<ul>
<?php
foreach($tags_arr as $tag){
echo '<li>'.$tag.'</li>';
}
?>
</ul>
<?php wp_reset_query(); ?>
更新(简化):::
要制作来自特定类别的标签列表,此代码要好得多(只需更改类别名称):
::最近因为循环错误再次更新::
<ul>
<?php
query_posts('category_name=html');
if (have_posts()) : while (have_posts()) : the_post();
if( get_the_tag_list() ){
echo $posttags = get_the_tag_list('<li>','</li><li>','</li>');
}
endwhile; endif;
wp_reset_query();
?>
</ul>
即使遇到困难,我也可能有解决方案,如果有新解决方案出现,请更新。
【问题讨论】:
-
你如何整合这个例如在侧边栏中?您能否更具体地说明步骤、粘贴代码的位置等?谢谢!
-
代码底部唯一依赖的是您输入的 category_name。您可以将其粘贴到任何您想要的位置。如果您希望它显示由 ONE 类别组成的标签列表,请将类别名称放在显示“html”的位置并将其粘贴到您想要的任何位置。
-
这不会忽略重复项。每次出现在帖子中时,它都会显示标签。有谁知道忽略重复并仅显示唯一标签的方法?
标签: wordpress categories tag-cloud