Online Magazine Tutorial: Part 2

This post is part of a series (Online Magazine):

  1. Online Magazine Tutorial: Part 1
  2. Online Magazine Tutorial: Part 2
  3. 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.

This post is going to cover 2 points in my series. Tiered sponsorship levels, and random sponsorship links. We’ll share a little more code and hopefully enlighten a few readers to the possibilities of Wordpress.

Tiered Sponsors

Sponsor CategoriesOne feature of the current incarnation of the magazine that was important was the ability to let sponsors buy in at different levels. This was accomplished in much the same fashion as the banner and secondary categories from Part 1. We just created three categories, one for each of three tiers, and then made them subcategories of a main sponsor category. So a second tier sponsor would be categorized first as “Sponsor” and second as “Second Tier”. The ID’s provided by these categories provide many options for weighting the posts in “random” queries and ordering lists of sponsors. At present each sponsorship tier allows for a different amount of copy/picture size and is pretty visible from the front end. At this point the magazine isn’t making much behind-the-scenes use of the weighting features, but it’s there for future expansion.

Random Sponorship Links

You’ll notice at the top of the site there are 3 rotating sponsors with links to their directory ad. There is an additional category under Sponsors called “Featured Sponsors” from which these are pulled. Featured sponsors is a more behind-the-scenes determination and needed to be separate from the buy-in level of the sponsor. Having a separate category also allows for featuring and exclusion of sponsors across all tiers.

The code for randomly selecting sponsors from one category involves first doing a query and picking up all of the post IDs in that category and placing them in an array. Then by selecting 3 random posts from that array and returning them to the main function, we can randomize the display of sponsors. At the top of the home.php file there is this function:

< ?php
function rand_sponsors() {
	global $wpdb;

	$randomsponsors = array();
	//query to get known post IDs from category 5
	//(the featured sponsors category)
	$sql = "SELECT DISTINCT ID from $wpdb->posts";
	$sql .= " INNER JOIN $wpdb->post2cat ";
	$sql .= " on ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
	$sql .= " AND ($wpdb->post2cat.category_id = 5) ";
	$sql .= " ORDER BY ID ASC";

	$result = $wpdb->get_col($sql);

	// a query to grab randomized posts from known IDs
	$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 the IDs
	return $randomsponsors;
}

?>

This returns an array that is handled in the main body of the page:

< ?php $rp = rand_sponsors();
foreach ($rp as $p) {
	$post = get_post($p);
	$query = "p=" . $post->ID;
	query_posts($query);
	if (have_posts()) : while (have_posts()) : the_post(); ?>
		Loop Processing Goes Here
< ?php endwhile; endif; } ?>

In our case, this was placed in a “featuredSponsors” div and the loop runs inside of an unordered list, with the loop generating a list item for each sponsor containing the title and permalink for the post connected to the sponsor.

Hopefully that all makes sense and will help a few people with similar projects. In the third and last part of the series I’ll show how Ultimate Tag Warrior ties together all the pieces and provides the ability to show related sponsors in each article with a fallback to random featured sponsors if it can’t find any that are related.

» » »
  1. Ryan 03.21.07 / 10pm

    It’s really great that you’re publishing all these how-to articles. I’m really getting a lot out of reading your site. Thanks.

  2. brett 03.22.07 / 5am

    I’m happy to do it. I’d be up a creek in a lot of cases if other people weren’t sharing their own experiences like this ;).

  3. David Clark 03.22.07 / 7am

    Brett,

    I’m a huge fan of your work — esp your tm bundles and support.

    I may not understand yoyour situation fully, but the last time I needed to come up with a way to get x random records of an sql query, I found a way to do it entiroly in the sql:
    order by RAND() LIMIT 0,x (where x is the # of random records you want). I used this for http://www.rwanderman.com/index.php?x=browse
    If I am missing something sorry — Hope this helps

  4. Brett Terpstra 03.22.07 / 7am

    I’ll play with that tonight and see if I’m overdoing things. Thanks for the tip!

  5. Circle Six Blog » Blog Archive » Online Magazine Tutorial: Part 3 04.17.07 / 8am

    […] Tutorial: Part 3 This post is part of a series (Online Magazine):Online Magazine Tutorial: Part 1Online Magazine Tutorial: Part 2Online Magazine Tutorial: Part […]

Have your say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>




Safari hates me