Being a software developer and programmer by trade, sometimes I find it easier to crack open my Zen Develpoment Enviornment and hack my Wordpress theme rather than search for an appropriate plugin that may or may not work. Today, I was fooling around with optimizong my blogs and wanted to add a “Most Popular Articles” list to my blogs sidebar so I came up with a quick hack. In addition to the sidebar list, I decided to go a step further and add a custom page that listed the 20 most popular articles based on user comments. Wordpress doesn’t natively support PHP code in posts or pages and I wasn’t in the mood to tinker with the phpexec plugin which didn’t seem to work, so I spent another 5 minutes to create another simple hack.
I’ve decided to share these hacks with any of you Wordpress bloggers out there who might be interested. So here’s how to add a simple “Most Popular Articles” box to your blog.
Hacking the "Most Popular Articles List"
Open your theme’s file wherever you’d like to place the list. In this case, I edited my sidebar.php file, however in some themes you may have to edit your index.php file. Insert the following code in the spot you want to see the list:
<?php
$sql = "SELECT post_date, post_title, post_name, comment_count from wp_posts ORDER BY comment_count DESC LIMIT 7";
$searches = $wpdb->get_results($sql);
echo '<table border="0" cellpadding="0" cellspacing="1" width="180"><br />
<tr><td style="border-bottom:#A27C08 1px solid;"><b>Most Popular Articles</b></td></tr>';
foreach ( $searches as $searchz ){
$dtp = explode(" ", $searchz->post_date);
$tdstr = preg_replace("/\-/", "/", $dtp[0]);
echo '<tr><td><img src="/images/green_star.png" alt="green star" style="margin: 0px 0px 0px 2px;">
<a href="http://www.yourdomain.com/' . $tdstr . '/' . $searchz->post_name .
'" title="' . $searchz->post_title . '">' . $searchz->post_title . '</a></td></tr>';
}
echo '<tr><td style="border-top:#A27C08 1px solid;"><br />
<a href="http://www.yourdomain.com/most-popular-articles/" title="Most Popular Articles">
20 Most Popular Articles…</a></td></tr></table>';
?>
|
* Please note, this example is assuming you have Permalinks turned on. You must edit “yourdomain.com” to reflect your domain and make sure the path to your blog is correct. In this example the blog is installed in the root directory of the webserver. You can easily edit the number of articles by changing “LIMIT 7″ to the number of posts you would like to list. The “green-star.png” bullet is optional and you can either remove the
tag or if you’s like to use your own image, you can change it to whatever you like. If you need a bullet image, there are quite a few nic ones for free download at Bullet Madness.
Hacking the "Most Popular Articles Page"
First, login to your Wordpress installation and in your dashboard, select Manage > Pages > Create New Page and create a new page titled “Most Popular Articles”. Add whatever heading or text you want at the beginning of the page and publish the page.
After your Most Popular Articles Page has been saved, Open your themes index page where the content loop is and find this line (note: if you’re using default theme on the newest Wordpress release the file you need to edit is single.php) :
|
<?php the_content(__('(more…)')); ?>
|
and replace it with this…
|
<?php
the_content(__('(more…)'));
/* Handle the most Popular Posts Here */
if ( $_SERVER['REQUEST_URI'] == '/most-popular-articles/' ){
$sql = "SELECT post_date, post_title, post_name, comment_count from wp_posts ORDER BY comment_count DESC LIMIT 20";
$searches = $wpdb->get_results($sql);
echo '<table border="0" cellpadding="0" cellspacing="1" width="500"><br />
<tr><td colspan="2"><b>20 Most Popular Articles</b><br /><small>* Ordered by number of comments<small><br /><br /></td></tr>';
foreach ( $searches as $searchz ){
$dtp = explode(" ", $searchz->post_date);
$tdstr = preg_replace("/\-/", "/", $dtp[0]);
echo '<tr><td>' . $searchz->comment_count . '</td><td><img src="/images/green_star.png" alt="green star" style="margin: 0px 0px 0px 2px;">
<a href="http://www.yourdomain.com/' . $tdstr . '/' . $searchz->post_name . '" title="' . $searchz->post_title .
'">' . $searchz->post_title . '</a></td></tr>';
}
echo "</table><br /><br />";
}
/* End Popular Posts Here */
?>
|
As with the previous step, you must edit “yourdomain.com” and do what you want with the bullet image. How this works is that the code will look for a single page ending with “/most-popular-articles/”, so if you wanted to name your custom page something else, you must edit the code above to reflect your choices.
That’s it, quick and simple hack to add a “Most Popular Articles” list and page to your Wordpress blog.
If you found this post helpful, please take a moment and Digg It and/or post a comment. The “nofollow” attribute is turned off on my comments page, so you’ll not only be letting me know you found this article useful, you’ll also be getting a nice backlink.
Cheers !!!