Displaying Related Category and Author Content in Wordpress

Displaying Related Category and Author Content in Wordpress

June 11, 2008 ( 21 )

View any single-post page from Mimbo Pro and you’ll notice two sidebar modules called “More from this category” and “More from this author”.

Why Is It Useful?

Pretend you’d discovered that story (”Kenya tourism, economy devastated by violence”) via random Google search. You might finish reading, know nothing else about the site, and hit your ‘Back’ button. As a designer, you want to help users explore topics in a forward or sideways motion from anywhere on the site. You can accomplish this by pulling in related content and providing logical jumping-off points, without requiring users to work backward.

Online newspapers like The Times use this principle. This article on Hillary Clinton is supported by smaller modules like political cartoons and related election coverage:

Politically-minded readers are now immersed in a topic they already enjoy, and are more likely to dig deeper into a whole section of the website rather than just one story. Good information-design connects the page to the interests of the user. Think of Pandora’s scrolling interface and the way it begins to ‘know’ your musical taste, compelling you to visit more often.

The Code

Here’s a simplified version of the code Ben wrote for Mimbo Pro, which must be included outside of the loop:

<?php

  //Gets category and author info
  global $wp_query;
$cats = get_the_category();
  $postAuthor = $wp_query->post->post_author;
$tempQuery = $wp_query;
  $currentId = $post->ID;

// related author posts
  $newQuery = "posts_per_page=5&author=" . $authorPosts;
  query_posts( $newQuery );
$authorPosts = "";
  $count = 0;
if (have_posts()) {
  while (have_posts()) {
  $count++;
  the_post();
  if( $count<4 && $currentId!=$post->ID) {
  $count++;
  $authorPosts .= '<li><a href="' . get_permalink() . '">' . the_title( "", "", false ) . '</a></li>';
  }
  }
  }

// related category posts
  $catlist = "";
  forEach( $cats as $c ) {
  if( $catlist != "" ) { $catlist .= ","; }
  $catlist .= $c->cat_ID;
  }
  $newQuery = "posts_per_page=5&cat=" . $catlist;
  query_posts( $newQuery );
$categoryPosts = "";
  $count = 0;
if (have_posts()) {
  while (have_posts()) {
  the_post();
  if( $count<4 && $currentId!=$post->ID) {
  $count++;
  $categoryPosts .= '<li><a href="' . get_permalink() . '">' . the_title( "", "", false ) . '</a></li>';
  }
  }
  }
  $wp_query = $tempQuery;
  ?>

Then inside the loop, call the functions like this:

<h4>More from this category</h4>
<ul>
<?php echo $categoryPosts; ?>
</ul>

<h4>More from this author</h4>
<ul>
<?php echo $authorPosts; ?>
</ul>
How’s it Work?

This whole task would be easy if we were pulling from a single static category or author—the query_posts function offers simple parameters. But pulling that information dynamically is slightly trickier.

Ben explains further:

All we need is work out which category/author the post is in and use that data to get the information. 5 posts are grabbed - then, when looping through the posts, we double check to make sure that the current post is not being linked to redundantly (this feature is not currently in Mimbo Pro but will be in the next update).

Because of the layout of the theme, we need to do all of this before the post is displayed. At the time I found the easiest method was to save the current page’s data as a new variable ($tempQuery = $wp_query;), do all the work, and then reassign the values so that the page can be updated as normal. This works great, but if I was to write it again I would use a new instance of the wp_query object, as described here by Ronald Huereca.

No matter how you code it, supporting your posts with related content is valuable to readers who want additional context and to bloggers who want a stickier site.

Popularity: 2% [?]

21 Responses
  1. david said:

    Two things: a question and a thought.

    Does this check to make sure that posts aren’t duplicated between the category query and the author query? My quick examination of the code makes me think no, but I could certainly be missing something.

    I’m also rather intrigued by the possibility that with a few modifications this code could be used for fetching posts that share a tag (or tags) with the current post. Categories and tags could be a useful pair for blogs with a single author.

    Oh, and thanks for giving us “behind the scenes” details about Mimbo Pro. I wish more premium themes developers were as generous with such thoughts as you’ve been.

  2. Darren said:

    Does this check to make sure that posts aren’t duplicated between the category query and the author query?

    Not yet, but that’s something we’re working on for the next version.

    I’m also rather intrigued by the possibility that with a few modifications this code could be used for fetching posts that share a tag (or tags) with the current post.

    Exactly, I think there’s a handful of other useful things to do with it. Let us know if you implement anything.

    Oh, and thanks for giving us “behind the scenes” details about Mimbo Pro.

    No problem, I hope to do more of it. I don’t think any of this stuff should be treated like it’s shrouded in mystery. I hope the tutorials can spark other ideas in the WP community.

  3. CP said:

    Great tutorial Darren.

    I gave this a shot on Knoxify.com without any luck. Perhaps I’m missing tweaks on my end? Thanks.

    CP

  4. Darren, you talked about the timesonline relevant and thought about the design of the mimbo pro, could be lighter than the present, the style and timesonlie nyt.
    The Mimbo Pro has the potential to become one of the most complete for the Wordpress, if you enlarge more a home, provide a color scheme cleaner and mild, include some special pages as the WP Remix and the Revolution.
    In all, he is a great issue!
    even more …

  5. Erika said:

    I love this. I was just trying to figure out if there was any blatant way to manipulate what’s in the codex to do something similar to this, or if there was a plugin, but for 2.5.1? No such luck (yet.)

    Thanks for the share. Gives a little bit of insight for how I should be thinking about this!

  6. Sunny said:

    @ david

    You can avoid duplication, hack the above code and include

    $do_not_duplicate = $post->ID;

    right after the call for the_post(); and then use

    == $do_not_duplicate

    right after if( $countID

    Code is untested, so do it at your own risk (hint- backup your files).

  7. Sunny said:

    @ Erica,

    There is a plugin that does more than pull content based on category/author, it uses keyword density to match posts. Just Google “wordpress+related+entries”.

    Also, WordPress might bring this feature to WP 2.6.x

  8. Darren said:

    @Sunny: thanks for the recommendations!

    BTW, I haven’t forgotten about your question about capitalizing the ‘P’ in WordPress, but I still don’t have a compelling answer ;)

  9. rhuereca said:

    Hey y’all,

    I’m using Mimbo Pro, and I use the above category/author technique in my single posts.

    I wrote an article a month ago that solves the duplicate content issue and am successfully using it with Mimbo Pro.

    How to: Avoid Duplicate Posts

  10. Darren said:

    Thanks Ronald, I’m looking into implementing that now.

  11. rhuereca said:

    @Darren,

    Sounds good. If you need the code I used, I can send it over.

  12. Jenny said:

    This is pretty nifty. I wanna try using it in my new theme.

  13. Francis said:

    Hi Darren

    I did try to implement that technique (tried both in and out the loop) but nothing appeared on my blog… :-( Did I miss something important ?

    Thanks for your help

  14. mGz said:

    Thanks a lot. Great idea. I will try to implement this on mine blog :).

    Regards,
    mGz

Trackbacks
  1. WordPress Weekly Episode 20 | Jeffro2pt0:

    [...] Displaying Related Category and Author Content In WordPress [...]

  2. WordPress Talk - June 15, 2008:

    [...] Displaying Related Categories and Content in WordPress - Darren Hoyt shares the code he used to make this feature in his popular Mimbo Pro theme. Want automatic updates? Subscribe to our RSS feed or Get E-mail Updates Automatically! Digg This! | Stumble it! | Add to Del.icio.us | | Print This! | SHARETHIS.addEntry({ title: “WordPress Talk - June 15, 2008″, url: “http://hackwordpress.com/wordpress-talk-june-15-2008/” }); [...]

  3. links for 2008-06-15 | JeremiahTolbert.com:

    [...] Displaying Related Category and Author Content in Wordpress | Darren Hoyt Dot Com PHP code examples (tags: wordpress theme webdesign) [...]

  4. Related Category and Author Content in Wordpress | David Bisset: Web Designer, Coder, Wordpress Guru:

    [...] This is a good technique that the big blogs do, and it’s helpful to see how you can apply this for your projects. Display a particular post and try to show related (by topic or author) posts or information to keep the user on the site. Tags: PHP, Wordpress [...]

  5. Cool coding tips from around your WordPress community - part 1 — WP Project:

    [...] Hoyt explains the internal workings of the More from this Category option in his Mimbo Pro theme. As a designer, you want to help users explore topics in a forward or [...]

  6. Reasons for keeping your web portfolio short and sweet : Tracey Grady Design:

    [...] in one particular type of design (e.g. your website work, as opposed to your logo designs). Darren Hoyt talks about using this technique to extend the time visitors stay on a blog, but I expect it could [...]

  7. WordPress Weekend Resources - June 20, 2008 | Theme Lab:

    [...] Displaying Related Category and Author Content in Wordpress - This post by Darren Hoyt, author of the popular Mimbo theme, reveals code from the Pro version to display related content. Displaying related content to the category or author is a great way to keep your visitors on your site. [...]

Leave a Reply