0

I am trying to output all the tags (custom taxonomy) without link, and add special class to current post tags. For example: if there are 10 tags but current post has 3 of them applied, then it should display all the 10 tags without link but only 3 tags should have special class.

I am currently using:

        $terms = get_the_terms( $post->ID, $tax );
        $tag_list = implode(',', wp_list_pluck($terms, 'term_id') );
        wp_tag_cloud( array( 'taxonomy' => $tax, 'include' => $tag_list) );

which is just showing tags with link for current post.

2
  • Just want to update my question, but don't know how to do it. I want to display all the tags without link, but hightlight (add special class or span) to tags for current post. Commented May 27, 2015 at 15:20
  • 1
    Use the edit link, left-hand side, bottom of the question. Commented May 27, 2015 at 15:26

2 Answers 2

1

If you trace the code through the source, you will notice that the links are created by get_term_link() and that there is a filter in that function called term_link. The second parameter of that filter contain the object ID which, as near as I can tell, is only set if the tag is part of the current post. Leveraging that information:

function term_link_wpse_189584($termlink, $term) {
//   var_dump($termlink, $term, $taxonomy);
  if (!isset($term->object_id)) {
    $termlink = 'PULL';
  }
  return $termlink;
}
add_filter('term_link','term_link_wpse_189584',10,2);

function tag_cloud_wpse_189584($return) {
  $pat = '|<a.*PULL[^>]*>([^<]*)</a>|';
  $return = preg_replace($pat,'$1',$return);
  return $return;
}
add_filter('wp_tag_cloud','tag_cloud_wpse_189584');

$tax = 'post_tag';
wp_tag_cloud( array( 'taxonomy' => $tax ));
0

I was able to achieve this by following code:

        $allterms = get_terms( $taxonomy );
        $pterms = get_the_terms( $post->ID, $taxonomy);
        $tag_list = implode(',', wp_list_pluck($pterms, 'term_id') );
        $t = explode(',', $tag_list);
        echo '<div class="tag-cloud">';
        foreach ($allterms as $term)
        {
            if (in_array($term->term_id, $t))
            {
                echo '<span class="stag active">'.$term->name.'</span>';
            }
            else
            { echo '<span class="stag inactive">'.$term->name.'</span>'; }
        }
        echo '</div>';

Your Answer

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

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