6

Is this feature built into wordpress? i didnt see anything within the codex.

codex.wordpress.org/Function_Reference/wp_tag_cloud

I have a few pages that are category specific and i would like to show all the tags associated with those posts.

I did find this, but im not sure if its proper or if a better way exists (source)(old method!!!!):

<?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(); ?>

UPDATE( simplified ):::

to make a list of tags from a specific category this code is much better(just change the category name):

::Recently updated again because of a loop error::

    <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>

Even tough i may have a solution, please update this if a new one comes around.

3
  • And how can you integrate this e.g. in the sidebar? Can you be more specific on the steps, where to paste the code, etc? Thank you!
    – user761934
    May 19, 2011 at 23:50
  • The only thing the bottom bit of code relies on is the category_name that you put in. You can paste it anywhere you want. If you want it to show a tag list made from ONE category, put the category name in where it says 'html' and paste it anywhere you want. May 25, 2011 at 21:58
  • This does not ignore duplicates. It shows the tag every time it appears within a post. Does anyone know of a way to ignore duplicates and show only unique tags?
    – Nick
    Feb 22, 2016 at 17:24

4 Answers 4

2

I think the method you've found it's the only way you can achieve what you're looking for. Maybe you can modify some lines, but the concept is right.

at the moment i don't think there's a way to filter tags as you would using a core wordpress function.

3
  • its funny because on my search for an answer, people have been asking this question as far as 3 years back(on the wordpress forums). Dont know why they didnt include it yet. oh well, this will do Mar 16, 2011 at 19:08
  • 1
    maybe just because there are things more urgent to work on. if something can be achieved with 8 lines of code it's ok like that.
    – bluantinoo
    Mar 17, 2011 at 12:15
  • 1
    i managed to shorten the code with a wordpress function i found. it looks a bit better. I updated the question Mar 18, 2011 at 22:38
2

I did not get the code above to work my installation of WordPress. I did however manage to tweak it until it worked. Here is my tweak:

$catid = get_cat_ID(single_cat_title("",false));
$catobj = get_category($catid);
$catslug = $catobj->slug;
$all_tags_arr = array();
query_posts('category_name='.$catslug);
if (have_posts()) : while (have_posts()) : the_post();
    $posttags = get_the_tags();
    if ($posttags) {
        foreach($posttags as $tag) {
            $all_tags_arr[] = $tag -> term_id;
        }
    }
endwhile; endif; 

$tags_arr = array_unique($all_tags_arr);

$tagcloud_args = array(
    'include'   =>  implode(',',$tags_arr),
);

wp_tag_cloud( $tagcloud_args ); 
wp_reset_query();
2

Here is a much easier example.... Just change the category name and hey presto your done. The associated tags will print out in a list format.

<?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[] = $tag -> name;
        }
    }
    endwhile; endif; 

    //This snippet removes any duplicates.
    $tags_unique = array_unique($all_tags); 

    echo '<ul>';
        foreach($tags_unique as $unique) {
          echo  '<li>'.$unique.'</li>';
        }
    echo '</ul>';

    wp_reset_query();

?>
0

First of all, install the ACF plugin and create a taxonomy field. After adding this below code where you want to display the tags.

$queriedObj = get_queried_object(); 
$taxonomy = $queriedObj->taxonomy;
$term_id = $queriedObj->term_id;  

$current_tags = get_field('category_tags', $taxonomy . '_' . $term_id); //category_tags = ACF fieldname

if ( $current_tags ) {
  echo '<ul>';
  foreach ( $current_tags as $term ) {
      echo '<li>';
      echo '<a href="/product-tag/' . $term->slug . '">';
      echo $term->name;
      echo '</a>';
      echo '</li>';
  }
  echo '</ul>';
}
else{
    echo '<ul>';
    echo '<li>No Tag.</li>';
    echo '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.