PNSBXODYTQ ?
Subscribe to RSS feed

Home :.: About :.: Contact :.: Archives :.: Bookmarks :.: WishList


image

If you wonder how I displayed the above tag tables in wordpress in the DMOZ like directory form, I just took the dive into wpdb (so the database layer functions of WordPress) instead of meddling with the taxonomy functions:

The following function gets the amount of posts labeled with a specific tag:

   1: function fetch_tag_amount($tagid) {
   2:     global $wpdb;
   3:     $amount = $wpdb->get_var("SELECT count FROM $wpdb->term_taxonomy WHERE term_id=" . $tagid);
   4:     return $amount;
   5: }

The following function gets me the slug of the tag itself (I didnt have time to change the slugs on this site so they all still look like their original tag, with the change that I capatalized the first letter:

   1: function fetch_tag_slug($tagname) {
   2:                 global $wpdb;
   3:                 $slug = $wpdb->get_var("SELECT slug FROM $wpdb->terms WHERE name='$tagname'");
   4:                 return ucfirst($slug);
   5:             }

The following function fetches me the term id of a tag, which I need to retrieve the amount of postings:

   1: function fetch_tag_id($tagname) {
   2:     global $wpdb;
   3:     $tagid = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE name='$tagname'");
   4:     return $tagid;
   5: }

However if you play it nice you should actually use the non wpdb call:

   1: function get_tag_id($tag_name) {
   2:    $taxarray = is_term($tag_name, 'post_tag');
   3:    return $taxarray[term_id];
   4:  

But I didnt want to show ALL tags. I just wanted to show a specific amount of tags I was interested in. So I took the easy way and created a directory under my theme /inc/tags/TAGNAME each time I wanted to have a tag appear in the list I was interested in (manually), with the advantage that the contents of the directory could be used and are used for other nice things specifically tied to the tag.

So I loop the directory for the names supplied and add them to an array name “tagarray” …

   1: $tagarray = array();
   2:     $tagarraystring = array();
   3:     $dir = $includedirectory . 'tags/';
   4:     if (is_dir($dir)) {
   5:     if ($dh = opendir($dir)) {
   6:         while (($file = readdir($dh)) !== false) {
   7:                 if (filetype($dir . $file) == 'dir' && $file != '..' && $file !='.') {
   8:                     array_push($tagarray, get_tag_id($file));
   9:                     array_push($tagarraystring,$file);
  10:                 }
  11:         }
  12:         closedir($dh);
  13:     }
  14:     }

Inside my tag page I can now use code to check for the directory existence and do stuff, e.g.

   1: if (file_exists($tagdirectory . "header.php")) {
   2:                include ($tagdirectory. "header.php");
   3:             } 

to check if there is a specific header defined for the tag page, etc… it doesnt even have to be WordPress related, I can put complete applications in there or other weird stuff.

Furthermore to then display it in a table is as simple as:

   1: function taglinkline($tagname) {
   2:                 $returntagstring = '';
   3:                 $returntagstring .= '<a href="/about/' . $tagname . '">' . fetch_tag_slug($tagname) . '</a> (';
   4:                 $tagid = fetch_tag_id($tagname);
   5:                 $returntagstring .= fetch_tag_amount($tagid);
   6:                 $returntagstring .= ')<br />';
   7:                 return $returntagstring;
   8:             }
   9:  
  10:             function taggrouptitle($taggrouptitle) {
  11:                 $returntagstring = '<b>' . $taggrouptitle . '</b><br />';
  12:                 return $returntagstring;
  13:             }
  14:  
  15:             function tagColumn($taggrouptitle,$collectiontags,$rows) {
  16:                 $returntagstring = "<td bgcolor=\"#b0b0b0\" valign=\"top\" width=\"100px\" rowspan=\"$rows\">";
  17:                 $returntagstring .= taggrouptitle($taggrouptitle);
  18:                 foreach ($collectiontags as $collectiontag) {
  19:                     $returntagstring .= taglinkline($collectiontag);
  20:                 }
  21:                 $returntagstring .= '</td>';
  22:                 return $returntagstring;
  23:             }

And then create the directory by thinking on how to shape it, in my case I wanted to have a sort of DMOZ like indexed seperation, so here I did:

   1: <table hspace="0" vspace="0"><tr>
   2:    <?php
   3:     $tagstring='';
   4:     $tagstringtag .= tagColumn("Arts",                 array("art", "video", "music", "movie", "tv", "radio", "book", "comics", "writing", "graphics", "photo", "scifi"),2);
   5:     $tagstringtag .= tagColumn("Computers",        array("software", "hardware", "pda", "kiss", "synology", "wifi", "coding", "google", "microsoft", "firefox"),2);
   6:     $tagstringtag .= tagColumn("Internet",            array("links","blogging","wordpress","webmoney","webgoodie","socialweb","webtools","webdesign","hosting"),2);
   7:     $tagstringtag .= tagColumn("Home",                   array("household","finance","fashion","baby","health","travel","food","car","sonicare","gadget"),2);
   8:     $tagstringtag .= tagColumn("Games+Recreation", array("game","xbox","risk","cool","free","humor"),1);
   9:     $tagstringtag .= tagColumn("Society",               array("brights","skeptic","human","politics","green","celebrities"),1);
  10:     $tagstringtag .= tagColumn("Private",          array("private","bali","maarten","thisweblog", "brainstorm", "idea","indo","genealogy","nikon"),2);
  11:     $tagstringtag .= '</tr><tr>';
  12:     $tagstringtag .= tagColumn("Subs",             array("health","news","shopping","science","reference","it"),1);
  13:     $tagstringtag .= tagColumn("More",             array("lost","howto","wikipedia","history","review","xara"),1);
  14:     echo $tagstringtag;
  15:     ?>
  16: </tr></table>

Next step is to actually orden the tags within the specific tag pages. Which is a sort of tagcloud but only for the posts within a certain tag archive. More on that in any of the next postings.

Related posts

del.ico.us Del.icio.us

digg Digg

ekus Ekudos

reddit reddit

 coding

 dmoz

 php

 tag

 wordpress

November 10th, 2008