Category : annoying + programming + software
For those visiting last week, you may have noticed my extremely awesome “uh-oh” page informing you of my total screw-up. This happened after I decided to blindly take a chance and upgrade WordPress from 2.0 to 2.1. The entire experience was a bit like accidentally pulling your steering wheel off its column and throwing it into the back seet while driving down the highway. Somehow you still having most of the ability to steer but then only losing control and causing you to crash when the airbag on that steering wheel decides to magically deploy well after the fact.
What I suspect really happened was that upon upgrading, categories for all posts and links were merged incorrectly or not at all. This may have been the caused when I accidentally ran the WordPress upgrade script under 2.0.6 or 2.0.7 which caused those tables to get skipped upon migrating to 2.1. With a little luck however I had made a backup before this which contained 99.9% of the current database and it upgraded almost flawlessly. All that was left was fixing a small error in my template.
WordPress 2.1 also introduced a new template tag appropriately named “wp_list_bookmarks” to spit out the links from your blogroll. I decided to see if this worthy of replacing the MySQL query I’ve used this entire time found in the “wp_get_links” example. I was slightly impressed with it’s moderate flexibility on list items themselves, but the least desirable part is that there aren’t really any formatting options for the unordered list parent itself.
Given the minor amount of database changes, I simply tweaked existing query a bit and decided to also make it a conditional statement to make things backward compatible. While it’s not as elegant as a template tag, it works exactly as written because the output is entirely up to you. The only difference between this one and the original example is the query, and the obvious fact that it’s conditional. Simply, if “wp_list_bookmarks” is found as an option it assumes the version of WordPress is not 2.1.x and falls back onto the original.
<ul>...
<?php
if (function_exists('wp_list_bookmarks')) {
/* Look to see if this is WP 2.1 */
$link_cats = $wpdb->get_results
("SELECT cat_ID, cat_name FROM $wpdb->categories WHERE link_count > 0");
foreach ($link_cats as $link_cat) {
...(...)...
} else {
/* If it's not 2.1 run the old query. */
$link_cats = $wpdb->get_results
("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat) {
...(...)...
}}
?>
...</ul>
If you don’t need a 2.0.x condition to fall back on, then all you really need to do is tweak the original enough to suit your need. Additionally, “wp_get_links” my have to be swapped out for just “get_links” depending upon how you like your output. If I can remember and find the time I may eventually just turn this into a friendlier plug-in if nobody has already done so. Otherwise feel free to steal the idea.
Happy Hacking.