Display Drupal Taxonomy Like Craiglist Does

So you have a classified site build on top of drupal. Someday, you encounter this craiglist page and though that you would like to mimic that page. Don’t worry, I’ve been implementing in on my recent project and I would like to share it here.

We will use hook_menu like this:

function test_craiglist_menu() {
  $items['locations'] = array(
    'title' => 'Display classified ads like craiglist',
    'page callback' => 'test_craiglist_location_list',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'test_craiglist.pages.inc',

	return $items;

Then, Open test_craiglist.pages.inc and fill it with the following code:

function test_craiglist_location_list(){
	$listspan = 5;//define how many <td> you want
	//this is a temporary array that will hold all data for particular field.
	$arr_span = array();
	//this will hold how many row for each table field.
	$max_span = array();
	for($idxspan = 0; $idxspan < $listspan; $idxspan++ ){
		$arr_span[$idxspan] = array();
		$max_span[$idxspan] = 0;
	//to get a list of vocabulary
	//this could be optimized if I know a better way to get a vocabulary
	$installedVocs = taxonomy_get_vocabularies ();
	foreach($installedVocs as $voc ){
		if($voc->name == "Iklane Locations" ){
			$tree = taxonomy_get_tree($voc->vid );
			$ctre = count($tree);
			if ($tree && ($ctre > 0 )) {
				$cntidx = 0;
				$average = $ctre / $listspan;
				$leftover = $ctre - (floor($average) * $listspan);
				//fill in preliminary max value for each field
				for($idxspan = 0; $idxspan < $listspan; $idxspan++ ){
					$max_span[$idxspan] = floor($average);
				//for every leftover, spread it randomly to fields
				for($idxleftover = 0; $idxleftover < $leftover; $idxleftover++ ){
					$max_span[ rand(0, ($listspan - 1)) ]++;
				//this variable will hold maximum value from fields
				$max_table_row = max ($max_span);
				$curr_span = 0;
				$span_count = 0;
				//this snippet will fill out temporary array with term data
				foreach ($tree as $term) {
					$arr_span[$curr_span][] = str_repeat('&nbsp;&nbsp;&nbsp;', $term->depth) . 
						"<a href='?q=taxonomy/term/".$term->tid."'>" . $term->name . "</a>";
					if($span_count >= $max_span[$curr_span]){
						$span_count = 0;
				//now, fill our temporary array to real array that will be consumed by theme_table()
				//this code swap field to row
				for($disp_idx = 0; $disp_idx < $max_table_row; $disp_idx++ ){
					for($idxspan = 0; $idxspan < $listspan; $idxspan++ ){
						$inner_value = isset($arr_span[$idxspan][$disp_idx]) 
							? $arr_span[$idxspan][$disp_idx] 
							: '&nbsp;';
						$arr_return[$disp_idx][$idxspan] = $inner_value;
				return theme_table(array('', '', '', ''), $arr_return);

That’s it. Now you have taxonomy displayed the way craiglist does.

Author: Arief Bayu Purwanto

Hello, my name is Arief Bayu Purwanto, a 24 years old father of a beautiful daughter. Interested in online programming, linux, games, and reading. Currently working on kapanlagi.com as junior developer. I live in a relatively quite and cool place called Malang. I'm available for some freelance stuff as well as some consulting job. You can see my portofolio for some previous task I've finished and some other information related to my capability. Btw, I'm plurking here.