WordPress hacking

I’ve been hacking WordPress during my vacation. The results are now up and available here with some documentation on what was done (and how).

I’ve been on a vacation with nothing planned this week so I’ve finally had time to hack around with WordPress. The new template system offers a great platform on which building customized views is easy. I added a thumbnail (and link) to the current image in The Life of Jalo, a list of the five latest posts when viewing a single entry, a link to geocaches near us if you’re viewing a geocaching post, now playing information, and I jumped the folksonomy bandwagon. The rest of this post is fairly technical in content, so if code and web development aren’t your thing, you might want to move on.

The thumbnail from LoJ comes from a XML-RPC call to the code managing LoJ. It started from a test to see how XML-RPC really works. At least the Incution XML-RPC library is really easy to use.

Creating a list of the five latest posts in the sidebar required a visit to the WordPress Codex to see how the loop works. I created a secondary loop and output the links and titles. I limit the length of the titles to 35 characters to keep each title on a maximum of two lines. The following code does the trick:

<?php if (is_single()) : ?>
<li>recent entries:
  <ul>
    <?php $latest = new WP_Query('showposts=5'); 
      while ($latest->have_posts()) : $latest->the_post(); ?>
    <?php 
      $title = get_the_title(); 
      if (strlen($title) >= 35) 
        $title = substr($title, 0, 35) . '&hellip;';
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php echo $title; ?></a></li>
    <?php endwhile; ?>
  </ul>
</li>
<?php endif; ?>

For the now playing and folksony tag listing I use the custom fields that WordPress provides. If a one of the custom fields exists in a post, the value of the custom field is parsed and output in a meaningful way. I also create links to Technocrati’s tag pages according to the categories of the post as they are also a part of the folksonomy of the post.

While WordPress offers a function for checking if the category of the current page is the one given in a parameter (in_category()), the function uses the category id and not the name of the category. I wrote a function that takes a category name as its parameter and returns the first category that matches a case-insensitive search. The function is listed below.

    function in_cat($category) {
      $retValue = false;
      $categories = get_the_category();
      if (empty($category)) return false;
      for ($i = 0; $i < sizeof($categories); $i++) {
	$cat = $categories[$i];
	if (eregi($category, $cat->category_nicename) ||
	    eregi($category, $cat->cat_name)) {
	  $retValue = true;
	} 
      }
      return $retValue;
    } 

I’m thinking of making plugins out of some of these features that I made. I’ll just have to test them a bit more and check to see if there already are plugins that have the functionality I’m looking for (no sense in inventing the wheel twice).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.