Archive for the ‘Wordpress’ Category

Show BBpress Topic Tags from Inside WordPress

Saturday, June 5th, 2010

Show BBpress topic tags from inside WordPress.

I’ve been hosting a forum at http://diylilcnc.org using WordPress and BBrpess integration, so that WordPress and BBpress share user accounts. This is great for the most part; one drawback is that while you can use some of the WordPress functions inside BBpress, you can’t go the other way and call BBpress functions from inside WordPress.

For example, if you want to show a tag cloud with your forum’s topic tags on a WordPress post or page, you  might try to use the function bb_tag_heatmap, but that function only works if you’re on a BBpress page. You might also think of trying wp_tag_cloud, but since WordPress and BBpress are looking at different database tables for their tags, it won’t work either.

Here’s a pretty hacky way of making WordPress show BBpress topic tags when you’re not on a forum page. I put the code below into my WordPress theme’s sidebar.php.

global $wpdb;

$number_of_tags_to_show = 25;
$largest = 25; //largest font in points
$smallest = 4; //smallest font in points
$tag_page_url =  "http://www.mysite.org/forum/tags"; //url of your forum tags page

$taglist = $wpdb->get_results("select name, term_id from bb_terms","ARRAY_N");//$taglist[index][name][term_id]
for ($count = 0; $count < count($taglist); $count++)//use the term_id's to grab the count from bb_term_taxonomy {	 	if($taglist[$count][0])//if theres a tag term 	{ 		$query = "select count from bb_term_taxonomy where term_id = " . $taglist[$count][1]; 		$result = $wpdb->get_results($query, "ARRAY_N");
		$term_count = $result[0][0];
		$taglist[$count][2] = $term_count;
	}

} //now the array is formatted like so:  $taglist[index][name][term_id][count]

$taglist = sort2d($taglist, 2); //now the array is sorted with more popular tag at the front
$taglist = array_slice($taglist, 0, $number_of_tags_to_show);//truncate the array to show the correct number of tags
$taglist = sort2d($taglist, 0, 'asc', TRUE);//order the array alphabetically by tag

$min_count = 1;
$spread = 9;
if ( $spread <= 0 )
	$spread = 1;
$fontspread = $largest - $smallest;
if ( $fontspread <= 0 ) 	$fontspread = 1; $fontstep = $fontspread / $spread; while (list($tag_index) = each($taglist)) { 		 	$tag = $taglist[$tag_index][0]; 	$count = $taglist[$tag_index][2]; 	 	$fontsize = round( $smallest + ( ( $count - $min_count ) * $fontstep ), 1 ); 	$count > 1 ? $title = $count . " topics" : $title = $count . " topic";

	echo ' ';
	echo "\n";
}

function sort2d ($array, $index, $order='desc', $natsort=FALSE, $case_sensitive=FALSE)
{
   if(is_array($array) && count($array)>0)
   {
	 foreach(array_keys($array) as $key)
		$temp[$key]=$array[$key][$index];
		if(!$natsort)
		    ($order=='asc')? asort($temp) : arsort($temp);
	    else
	    {
		  ($case_sensitive)? natsort($temp) : natcasesort($temp);
		  if($order!='asc')
			 $temp=array_reverse($temp,TRUE);
	 }
	 foreach(array_keys($temp) as $key)
		(is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
	 return $sorted;
 }
 return $array;
}