Jerome's Keywords - Creating a Cosmos

Updated 13-Jan-2007 15:49

By far the coolest part of this plugin, you can create your own keyword (or tag) cosmos to compete with the big sites.

Generating the cosmos links is the easy part, but getting it set up just the way you like it is the tricky part. Let’s go through the steps:

  1. all_keywords() You can use this function anywhere in your template files like so: <?php all_keywords() ?>

    This will output an alphabetically-ordered list of all keyword links used in your site. Each link is assigned a class attribute based on how often it is used. By default, the links go to your local keyword search. You can customize the behaviour of this function (see below).

  2. Creating a cosmos page

    You can actually put your cosmos wherever you like: in the sidebar, header, following each post, etc. But this explanation sticks to how I created my cosmos page, which is only one of many possible methods.

    First, create a new page template file by copying an existing page.php template or whatever else you like. Make sure you put the all_keywords function somewhere in the new template. You’ll also need to include a page header comment at the top of the file. Here’s a sample cosmos.php template file I created for Kubrick so you can see what I mean. Make sure that you put this file in your theme directory.

    Next, create a new page from your WordPress admin panel. Give it a snappy title and change the post-slug to something like “tags” or ‘cosmos” (but not “tag”). Your new cosmos page template should show up in the page template drop-down. Select it, save your page and you’re done!

  3. Customizing it

    The all_keywords() accepts five optional parameters:

    • element — the text/html output for each keyword. The default value is <li class="cosmos keyword%count%"><a href="/tag/%keylink%">%keyword%</a></li> You can use any text you like and the function will automatically replace the following identifiers:
      • %keyword% is replaced by the actual keyword.
      • %keylink% is replaced by a URI-safe version of the keyword for use in links.
      • %flickr% is replaced by a Flickr-compatible URI (all lowercase alphanumeric) of the keyword for use in links.
      • %delicious% is replaced by a del.icio.us-compatible URI (no whitespace, encoded) of the keyword for use in links.
      • %count% is replaced by the number of times the keyword is used.
      • %em% and %/em% are replaced by html <em> and </em> tags (respectively), repeated as many times as the keyword is used. This is how Technorati styles their cosmos.
    • element_cat — same as element, but for categories. The default is blank ('’), which excludes categories from the cosmos.
    • min_scale — used for scaling the counts, this is the minimum value scaled to. The default is 1.
    • max_scale — used for scaling the counts, this is the maximum value scaled to. The default is false which implies no maximum.
    • min_include — the minimum count to include a keyword in the cosmos. The default is 0.
    Some examples:
    • all_keywords('<li class="cosmos keyword%count%"><a href="/index.php?tag=%keylink%">%keyword%</a></li>', '<li class="cosmos keyword%count%"><a href="%keylink%">%keyword%</a></li>') Links changed to work for sites that don’t use permalinks and also displays categories in the cosmos.
    • all_keywords('<li class="cosmos keyword%count%"><a href="/tag/%keylink%">%keyword%</a></li>', '', 1, 10, 5) Uses the default output but only shows keywords that appear at least 5 times and scales count values for those keywords between 1 and 10.
    • all_keywords('<li><a href="/tag/%keylink%" style="font-size: %count%px" >%keyword%</a></li>', '', 8, 20) Shows all keywords and styles the links so that the least-used have a font-size of 8 pixels and the most-used are 20 pixels.
    • all_keywords('<li class="cosmos keyword%count%"> <a href="/tag/%keylink%">%keyword%</a> <a href="http://flickr.com/photos/tags/%flickr%" title="%keyword on Flickr"><img src="flickr.gif" alt="Flickr" /></a> <a href="http://del.icio.us/tag/%delicious%" title="%keyword on del.icio.us"><img src="delicious.gif" alt="del.icio.us" /></a></li>') Displays the keyword link followed by icons linking to that keyword on Flickr and del.icio.us (supply your own .gif images)
    get_all_keywords() is a non-echoing version that simply returns an array of keywords and their counts, letting you do your own formatting.