A couple days ago I twittered that I’d be share a Dynamic Most Popular Pages By Category Wordpress hack. Its been in place on this blog for that time and I’ve noticed a decent improvement in the time people are spending on the site and the number of pages they visit.

Keep in mind, my increase is also a consequence of my categories being listed at the top of the page. People click through it often. I use it as a method to filter traffic and get visitors exactly where they want to be. Arguably, I need to go through some of my first posts and categorize them better, however, I’m sure many of you find yourself in the same position.

Adding a categorical most popular pages on category pages will provide you some relief until you can do that yourself. Either way, its still nice to have a another sorting method. And anything that helps the majority of your visitors improves the bottom line of most website’s goals.

Example

Web Design Category Page: http://robmalon.com/category/web-design/

Plugin & Version Requirements

Wordpress.Com Stats - Wordpress.Com Stats is one of the best, and the least load intensive statistics tracker you can get for Wordpress. Why? Because Wordpress themselves hosts it! See the graph to the right for an example of how it appears in your dashboard. This is where the data is pulled from to create the popular posts list.

Wordpress Popular Post - This “hack” is actually a modification to the Wordpress Popular Post Plugin. Perhaps they will update it with this functionality in a future release.

Wordpress 2.6 - I’m using this on a Wordpress 2.6.x install. I’ve heard that the popular post plugin does not work on Wordpress 2.5, but that may be an isolated issue. Give it a try and comment your results below.

WPPP - Wordpress Popular Posts Modifications

You can see the code changes below, but here is a link to my wppp.php modifications. I modified the 1.3.4 version of Wordpress Popular Posts. I’ll try and keep this updated, but you may be able to make changes to future versions of the plugin (with my additions) by using something like winmerge to migrate new/old code

The wppp.php file we’re modifying here can be found in: /wp-content/plugins/wordpresscom-popular-posts/wppp.php (if the plugin is installed)

  1. //Make changes to wppp.php version 1.3.4 and in this order for line numbers to be accurate
  2.  
  3. //LINE 20 - ADD AFTER: ,’title_length’ => ‘0′
  4. ,‘by_category’ => ‘0′
  5.  
  6. //LINE 61 - REPLACE: $howmany *= 2;
  7. $howmany *= 5;
  8.  
  9. //LINE 87 STARTS WITH: $results = $wpdb->get_results("
  10. //CONTINUES TILL 94 WHICH ENDS WITH: }
  11. REPLACE WITH:
  12. //RobMalon.com category hack
  13. $currentcat = $WPPP_defaults[‘by_category’];
  14. //Checks and toggle default query usage
  15. if (is_numeric($opzioni[‘by_category’]) && $opzioni[‘by_category’] > 0) {
  16. $results = $wpdb->get_results("SELECT * FROM {$wpdb->posts} p LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID LEFT OUTER JOIN wp_terms t ON t.term_id = r.term_taxonomy_id WHERE p.id IN (" . implode(‘,’, $id_list) . ") AND p.post_type = ‘" . ( $opzioni[’show’] == ‘pages’ ? ‘page’ : ‘post’ ) . "’ AND t.term_id = ‘" . $opzioni[‘by_category’] . "’");
  17. } else {
  18. //note ID changed to upper case
  19. $results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE id IN (" . implode(‘,’, $id_list) . ") AND post_type = ‘" . ( $opzioni[’show’] == ‘pages’ ? ‘page’ : ‘post’ ) . "’");
  20. }
  21.  
  22. $valid_list = array();
  23. foreach ( $results as $valid ) {
  24. $valid_list[] = $valid->ID; //note ID changed to upper case
  25. }
  26.  
  27. //LINE 209 - ADD AFTER: $opzioni['title_length'] = $opzioni['title_length'] !== NULL ? $opzioni['title_length'] : $WPPP_defaults['title_length'];
  28. $opzioni[‘by_category’] = $opzioni[‘by_category’] !== NULL ? $opzioni[‘by_category’] : $WPPP_defaults[‘by_category’];

Category Template Additions

Now you need to add a function call to WPPP_show_popular_posts in your /wp-content/themes/[themename]/category.php file. I’ve customized it in such a way that if there are less than 5 posts in a category, that it wont display a the popular category widget. You can see it and easily change that functionality in the code you see below.

  1. <?php if (have_posts()) :?>
  2. <h1 id="cat_h1">Category: <?php single_cat_title() ?></h1>
  3.  
  4. <?php
  5. $numposts = $wp_query->post_count;
  6. if (function_exists(‘WPPP_show_popular_posts’) && $numposts >= 5) { ?>
  7. <div id="popularbycategory">
  8. <h2 style="padding:0;">TOP 5 <?php single_cat_title() ?> Articles</h2>
  9. <div class="alreadyread">
  10. <?php
  11. $category = get_the_category();
  12. WPPP_show_popular_posts("show=posts&by_category={$category[0]->term_id}&title=&number=5&days=360&format=- <a href=’%post_permalink%’ title=’%post_title_attribute%’>%post_title%</a>");
  13. ?>
  14. </div></div>
  15. <?php } ?>

General Popular Posts

As a quick bonus, I thought I’d also give you the parameters of the general WPPP_show_popular_posts call that I’m using in the top left of all the pages on this site.

  1. <?php if (function_exists(‘WPPP_show_popular_posts’)) WPPP_show_popular_posts("show=posts&title_length=45&title=&number=6&days=360&format=- <a href=’%post_permalink%’ title=’%post_title_attribute%’>%post_title%</a>"); ?>

Extending The Idea

Note that the Popular By Categories hack I created was a rather quick fix. There’s more than one way to do it, and ways to make it more efficient/extendable. For example, pulling the popular categories for multiple delimited categories at once. That would come in handy for a those that assign multiple categories to a post and want to display a “Most Popular Posts Within This Posts Categories”. That’s a mouthful, and something I might do if its requested enough.

Leave a comment below with your results, examples, or questions.



Leave A Comment:

Comments RSS Feed

7 + 1 =

Custom Theme by Rob Malon | Content & Design © 2009 - Rob Malon [dot] Com.