Using wp_tag_cloud with only current post tag with special class

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.

Topic tags custom-taxonomy Wordpress

Category Web


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

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.