• Resolved Micemade

    (@anydog)


    I’m posting a solution to problem I had recently. I was using plugins Sensitive tag cloud and TDO-tag-fixes to display tags in current category, and both plugins stopped to work properly with my usage of new custom post types in WP 3. Both plugins display tags only associated with default post type – “post”.

    This is my solution to display tags in current posts category associated to default and custom post types. It’s not tag cloud, it’s unordered list of tags:

    <?php
    $categoryID = $cat; // category ID
    $blogURL = get_option('home'); // varible used for tag page link
    $category_posts = new WP_query(array(
    		'post_type' => array('post','company','product','any other custom post type'), //  <<--- CUSTOM POST TYPES
    		'cat' => $categoryID,
    		'posts_per_page' => -1
    		));
    if ($category_posts->have_posts()) : while ($category_posts->have_posts()) : $category_posts->the_post();
    	$posttags = get_the_tags();
    	if ($posttags) {
    		foreach($posttags as $tag) {
    			$all_tags_arr[] = array($tag->name , $tag->count, $tag->slug); // get tag name, slug and count (needed for sorting)
    		}
    	}
    
    endwhile; endif;
    
    if ($all_tags_arr) :
    
    	echo '<div class="category_tags"><h4>'.__('Most used tags in this category').'</h4>';
    
    	function sortBySubArray($a,$b){
    		return $b[1]-$a[1];
    	}
    	uasort($all_tags_arr, 'sortBySubArray'); //sorting descending with function usort, sort by tag count - most used tags first
    
    	foreach ($all_tags_arr as $tag) {
    			$tags[] = '<li><a href="'.$blogURL.'/tag/'.$tag[2].'">'.$tag[0].' ('.$tag[1].' ) </a></li>';
    		}
    
    	$tags_final = array_unique($tags); // remove duplicate posts
    	echo '<ul>';
    	foreach ( $tags_final as $tags ) {
    		echo $tags;
    	}
    	echo '</ul></div>';
    
    endif;
    ?>

    I hope someone will find this usefull, or even better, take this code, change it, make it better, and share it here … 😉

    Bye

Viewing 2 replies - 1 through 2 (of 2 total)
  • I found this post while searching for something similar: you saved me a lot of time. Many thanks 🙂

    Thread Starter Micemade

    (@anydog)

    No problem. I’m very glad that I can help someone, because I got a lot of help from great WP community. It’s good to give something back.
    Cheers !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Tags in current category with custom post types’ is closed to new replies.