admin管理员组文章数量:1122846
I'm trying to display some tags cloud in different locations of my Wordpress. I'm using a shortcode but I'm looking for a way to display only the tags from a specific category ID by adding this ID right in the shortcode like:
[tagscloud cat_id='2']
I made this but I'm stuck because I don't know how to filter/apply (not sure about the right term) the cat_id.
function wp_tags_cloud_shortcode($atts) {
extract(shortcode_atts(array(
'cat_id' => ''
), $atts));
$content = wp_tag_cloud( array(
'echo' => false,
'smallest' => 10,
'largest' => 10,
'unit' => 'px',
'number' => 0,
'format' => 'flat',
'orderby' => 'count' )
);
return $content;
}
add_shortcode( 'tagscloud', 'wp_tags_cloud_shortcode' );
I'm a beginner with php so any help will be much appreciate :)
I'm trying to display some tags cloud in different locations of my Wordpress. I'm using a shortcode but I'm looking for a way to display only the tags from a specific category ID by adding this ID right in the shortcode like:
[tagscloud cat_id='2']
I made this but I'm stuck because I don't know how to filter/apply (not sure about the right term) the cat_id.
function wp_tags_cloud_shortcode($atts) {
extract(shortcode_atts(array(
'cat_id' => ''
), $atts));
$content = wp_tag_cloud( array(
'echo' => false,
'smallest' => 10,
'largest' => 10,
'unit' => 'px',
'number' => 0,
'format' => 'flat',
'orderby' => 'count' )
);
return $content;
}
add_shortcode( 'tagscloud', 'wp_tags_cloud_shortcode' );
I'm a beginner with php so any help will be much appreciate :)
Share Improve this question edited Dec 24, 2017 at 18:13 dragoweb asked Dec 24, 2017 at 13:25 dragowebdragoweb 2494 silver badges13 bronze badges 3 |1 Answer
Reset to default 0I've finally found a working solution :
function tag_cloud_shortcode($atts) {
extract(shortcode_atts(array(
'cat' => ''
), $atts));
$query_args = array( 'cat' => $atts, 'posts_per_page' => -1 );
$custom_query = new WP_Query( $query_args );
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(
'echo' => false,
'smallest' => 10,
'largest' => 10,
'unit' => 'px',
'number' => 0,
'format' => 'flat',
'order' => 'count',
'include' => $tags_str
);
return wp_tag_cloud($args);
}
add_shortcode( 'tagscloud', 'tag_cloud_shortcode' );
Now I can use this shortcode with multiple category ID
[tagscloud cat=3,5]
Thanks to Jon Yablonski's post, Nate Beers's answer and code.tutsplus.com
本文标签: Display tags cloud from a specific category ID with a shortcode
版权声明:本文标题:Display tags cloud from a specific category ID with a shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290944a1928619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
cat
, in the function, you're looking forcat_id
. Pick one. – janh Commented Dec 24, 2017 at 16:58