Online Magazine Tutorial: Part 3
This post is part of a series (Online Magazine):
- Online Magazine Tutorial: Part 1
- Online Magazine Tutorial: Part 2
- Online Magazine Tutorial: Part 3
Note: This post is over a year and a half old. You may want to check later in this blog to see if there is new information.
The following links are auto-generated but may help you locate newer content:
In this, the third and final installment in the Online Magazine series, I’ll explain how we used Christine Davis’ famous Wordpress plugin, Ultimate Tag Warrior, to replace categories and tie in related sponsors to each post.
Better than Categories
So we used our categories up creating the fixed position banner and secondary articles, and the tiered sponsorship levels. How do we organize our articles and create the archives now? And how will we sort the sponsors into a directory of advertisers that can be categorized by type or location? Tags, of course. Ultimate Tag Warrior will let us add as many tags as we want to each article. That means we can assign an article to an infinite number of tag “categories”. Then, by assigning appropriate tags to each sponsor, we can develop relationships between article subjects and sponsors. It takes some creativity on the part of the person posting the articles, but UTW provides a dropdown menu of all of the previously used tags, so finding relevant tags isn’t too difficult.
My autotag mod for UTW makes things even easier, although I haven’t updated the online version for the new versions of Wordpress and Ultimate Tag Warrior. I’m using a slightly older version of the mod for this client, but it allows him to scan an article with the click of a button and get suggestions for tags from his own database. Very handy, but I should shut up because I don’t plan to support this or provide new versions for public release ;).
Relationships
The core of the related sponsors code is simply the built in UTW hook “ShowRelatedPostsForCurrentPost()”. This will rank posts based on their similar tags and return a given number of posts for our use. In single.php we check to see if we’re viewing an article (not a sponsor, as those are posts as well) and if so, get the sidebar:
< ?php if ( in_category(3) ) { get_sidebar(); } ?>
In the sidebar we call the UTW function:
< ?php UTW_ShowRelatedPostsForCurrentPost("posthtmllist",null,4,4) ?>
The first parameter returns an HTML list for us, the second we ignore, the third is our limit (how many sponsors we want to list at a time) and the last is the category to limit results to. In this case the last option is category 4, where our sponsors are.
Then it’s just a matter of styling the output! The plugin will return each result in a list item (li tag) and you can just make the call inside of a ul tag and style to your liking.
Fallback
What if there are no sponsors found? We can’t have an article on a site with paid sponsors display with no sponsors. So I took the code from the random featured sponsors and incorporated it into UTW. In ultimate-tag-warrior-core.php I modified the GetRelatedPosts function around line 1034 to include the following code:
[...]
if ($wpdb->get_results($q)) {
return $wpdb->get_results($q);
} else {
$randomsponsors = array();
$sql = "SELECT DISTINCT ID from $wpdb->posts";
$sql .= " INNER JOIN $wpdb->post2cat on ($wpdb->posts.ID = $wpdb->post2cat.post_id) AND ($wpdb->post2cat.category_id = 5) ";
$sql .= " ORDER BY ID ASC";
$result = $wpdb->get_col($sql);
$sql = "SELECT * FROM $wpdb->posts WHERE ";
srand((float) microtime() * 10000000);
$rand_keys = array_rand($result, 3);
$sql .= "(ID = '" . $result[$rand_keys[0]] . "')";
$sql .= " OR (ID = '" . $result[$rand_keys[1]] . "')";
$sql .= " OR (ID = '" . $result[$rand_keys[2]] . "')";
$sql .= " ORDER BY RAND()";
$randomsponsors = $wpdb->get_results($sql);
return $randomsponsors;
}
This will give us three random sponsors from the featured sponsors category if the previous query fails to return any results. Problem solved.
If there are any questions about this series, let me know. I know I didn’t go too in depth, but I just wanted to share a few of the more interesting points of the magazine. 
Have your say
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
No comments
Jump to comment form | comments rss [?] | trackback uri [?]