0

My tagcloud are placed in the sidebar (outside the loop).

I want to give the tags in the tagcloud a class if the current post is tagged with the tag. How do I do this? - something id wrong :(

This is my PHP-code:

$tags = get_tags();
$post_tags = get_the_tags($currentid);

if ($tags) {
  foreach ($tags as $tag) {
    if( (is_tag($tag)) || ($tag->name == $post_tags) ){ 
      echo '<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'" class="currenttag">'.$tag->name.'</a> '; 
      } else {
      echo '<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'"">'.$tag->name.'</a> ';
      }
    }
  }
0

get_the_tags will return an object of tags so you will have to make an array of the post tags and then see if the $tag->name is in the post tags array.

$tags = get_tags();
    $post_tags = get_the_tags($post->ID);

    /* Just make an array so you can use in_array function later on */
    $post_tag_array = array();
    foreach( $post_tags as $post_tag ){
        array_push($post_tag_array,$post_tag->name);
    }

    if ($tags) {
      foreach ($tags as $tag) {
        /* Then change to search this array for the $tag->name */
        if( (is_tag($tag)) || ( in_array($tag->name, $post_tag_array )) ){ 
          echo '<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'" class="currenttag">'.$tag->name.'</a> '; 
          } else {
          echo '<a href="'.get_tag_link($tag->term_id).'" title="'.$tag->name.'"">'.$tag->name.'</a> ';
          }
        }
      }

Hope this helps

  • No worries man, glad i could help – benpalmer Feb 21 '14 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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