phprockers-logo

How to get the post tags by category name in wordpress?

Paste this code wherever you want to get post tags for specific category name in wordpress.

<?php

$cat_name = "Test Cat";
query_posts('category_name='.$cat_name); //give the your category name here.

if (have_posts()) : while (have_posts()) : the_post();
$postTags = get_the_tags();
    if ($postTags) {
        foreach($postTags as $tag) {
            $all_tags[] = $tag -> name;
        }
    }
endwhile; endif;

if(!empty($all_tags)) {
    $unique_tags = array_unique($all_tags); //Removes the duplicates tags in the all_tags_arr array.
    echo '<pre>'.print_r($unique_tags, true).'</pre>';
} else {
    echo "No post tags are available for ".$cat_name." - category";
}

?>

0 comments:

Post a Comment