Get tags specific to a particular category on your WordPress blog

June 26, 2009 at 9:04 am

Althought WordPress allow you to do lots of things by default, it didn’t allow you to display tags based on a selected category. Here is an useful code to be able to do so.

Just paste this code wherever you need to display the list of tags that are specific to a particular category. Don’t forget to replace the category name on line 2.

<?php
	query_posts('category_name=work');
	if (have_posts()) : while (have_posts()) : the_post();
        $posttags = get_the_tags();
		if ($posttags) {
			foreach($posttags as $tag) {
				$all_tags_arr[] = $tag -> name; //USING JUST $tag MAKING $all_tags_arr A MULTI-DIMENSIONAL ARRAY, WHICH DOES WORK WITH array_unique
			}
		}
	endwhile; endif; 

	$tags_arr = array_unique($all_tags_arr); //REMOVES DUPLICATES
	echo '<pre>'.print_r($tags_arr, true).'</pre>'; //OUTPUT FINAL TAGS FROM CATEGORY

?>

Thanks to WordPress Forums for the tip!

† RIP Michael Jackson †