neverblueads logo MarketLeverage logo Hydra Network logo neverblueads logo
logo  

A discussion of earning with the Best Affiliate Programs, SEO, Wordpress Blogging and General Motivational Ideas for Internet Publishers and Affiliate Marketers.


feedburner

Blog Icon Wordpress Hack : Most Popular Articles

Posted in Blogs and Blogging by Dave on July 24th, 2007

Being a software developer and programmer by trade, sometimes I find it easier to crack open my Zen Develpoment Enviornment and hack my 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 .

Cheers !!!



Like this post? Subscribe to AffiliateBestPrograms RSS feed and never miss a post !!


del.icio.us:Wordpress Hack : Most Popular Articles newsvine:Wordpress Hack : Most Popular Articles furl:Wordpress Hack : Most Popular Articles reddit:Wordpress Hack : Most Popular Articles blogmarks:Wordpress Hack : Most Popular Articles Y!:Wordpress Hack : Most Popular Articles smarking:Wordpress Hack : Most Popular Articles magnolia:Wordpress Hack : Most Popular Articles segnalo:Wordpress Hack : Most Popular Articles gifttagging:Wordpress Hack : Most Popular Articles




28 Responses to 'Wordpress Hack : Most Popular Articles'

Subscribe to comments with RSS or TrackBack to 'Wordpress Hack : Most Popular Articles'.

  1. Miranda Vasquez on July 24th, 2007

    Works like a charm and so easy to setup !!!!!!!! Thannnnxxxx Dave !!!!! I digg*ed your article.

    one question - If I update my wp software will the hack get wrote over ?

  2. Dave on July 24th, 2007

    Thanks. I’m glad you liked it. If anybody has any questions about the code and getting it to work in your Wordpress version feel free to ask.

    A couple of notes:

    In the newest Wordpress default theme, the actual file you will be editing for the second set of code is the file single.php which is where the code for single posts is kept.

    To answer Miranda’s question, No, if you update your Wordpress software, this hack will still be in place because you’re editing the theme files and not the Wordpress core code.

  3. recep on July 25th, 2007

    it doesnt worked for me :S

  4. Vaas on July 25th, 2007

    This is a fabulous hack for WP. I know everybody loves to use WP for their blogs and getting another page to be indexed is always a plus.

    It enhances the overall feel of a blog when you have a most popular page in an older blog!

    great work

  5. Dave on July 26th, 2007

    Thanks for the comment Vaas. I’m glad you liked the hack. A friend of mine pointed out that linking to your best or user favorite posts on the landing page of your blog does add to your internal pages getting indexed and/or getting PR passed to them.

  6. Jakob on July 30th, 2007

    This is exactly what I expected to find out after reading the title ress Hack : Most Popular Articles. Thanks for informative article

  7. […] can find the code at his site here Did You Enjoy this Post? Subscribe to Dollars […]

  8. moneymakerblogs on July 31st, 2007

    Awesome post. Thanks for this. I have a few blogs on WP and can’t wait to try this hack on them.

    :)

  9. codingpad on July 31st, 2007

    I dugg this and left a comment :). It’s definitely a useful hack that I will try on my blogs that run WP. As a PHP beginner, this is good for me because I can look through the code and maybe learn something.

    thanks much for sharing!!

  10. Edgar on July 31st, 2007

    Hello! Good Site! Thanks for the hack. I’ve already added it to my blog and it works great.

  11. Eric on August 4th, 2007

    This is exactly what I expected to find out after reading the title ress Hack : Most Popular Articles. Thanks for informative article

  12. Chilmark on September 15th, 2007

    You did a lot of work for this article. You have a nice quality blog here. Ill just make a comment so it bumps this great post higher up on your “most populars widget”. ;)

  13. holli on October 14th, 2007

    Golly. I feel so fortunate to have found this. Bonus! Thanks for the great work. Are you married? =)

  14. simple67 on October 22nd, 2007

    […] here for more No Comments so far Leave a comment RSS feed for comments on this post. TrackBack […]

  15. Colleen on October 29th, 2007

    Thanks for this, I am having one little problem though, I am using permalinks but it’s not using what I set, I only have the year in my permalinks and this hack is grabbing the whole date in the url and causing the link to go to a 404 error page….

  16. Colleen on October 29th, 2007

    It could be because we’ve put the code in our header, not the sidebar, this is for my client, so I am trying to get it fixed as his site is live.

  17. Dave on October 29th, 2007

    The code can be put in any section of the site as long as it’s within the tags. Can you post a link to the site so I can have a quick look at it? You might need to write a quick SQL statement to fix the old links to permalinks. If I can see the site, I can help you better.

    Cheers,
    Dave

  18. One Eyed View on November 23rd, 2007

    Hi Dave,

    This is a great hack but as I was getting ready to start editing my sidebar it occurred to me that this might not work if I’m using Widgets. I will experiment more tomorrow, but any thoughts on using this while using Widgets would be greatly appreciated.

    Thanks,

    One Eyed View

  19. Jamster on December 3rd, 2007

    I am using widgets also and I put this hackon my blog and everything is working fine. No problems.

  20. Spyware List on December 9th, 2007

    Howdy Expert, I am glad I pressed harder enough until I found spyware list, because this post on ress Hack : Most Popular Articles was extremely helpful. Just last Sunday I was pondering on this quite a bit.

  21. How To Hack A Wordpress Blog on January 13th, 2008

    I gather always different opinions about web 2.0 and how to market with the different mindset of the \”new\” Internet. Will social interaction and networking really make the web different? Thanks for your thoughts on this!

  22. Chris on February 5th, 2008

    Awesome, thanks! Do you know how I could exclude certain posts from the list?

    Some articles that are more “newsy” may have generated a lot of comments when they were posted but they’ve lost their relevance in time.

  23. arrehovavaive on March 24th, 2008

    At first I thought this was just another article saying the same old things but it was interesting enough for me to read through the whole article and try to apply it to my blog. I wanted to say “Thank You” for such helpful articles.

  24. Terri on August 7th, 2008

    It worked, thank you. Just what I was looking for. I had to change the URL to get rid of the date part in my links, but it was sooo easy! Kudos and thanks again.

  25. pass over on August 8th, 2008

    nice hack :)

  26. Douglas - Murfreesboro SEO on September 2nd, 2008

    Thanks for this post, I do not currently use wordpress as I believe it is written in PHP which I do not know, I am a ASP/SQL person so thats why I use, I do though hope to get some time to learn PHP. Dave why do you call them hacks VS. Mods? Thanks!

  27. Web Services on November 13th, 2008

    Hello,
    Awesome post.Thanks for the post.Very informative and nice post.Thank you again.You really did a great job.

  28. Emily - hacker tools on March 26th, 2009

    Thanks for sharing the idea. I tried it in my blog. I want to know some more details about protection from hacking too.

Leave a Reply



This is a dofollow blog, however, if you wish to have your comment approved, please use a human name and not something like "free hosting" or "adsense help" in the name field. I will no longer approve comments that are made solely for the purpose of building backlinks at my expense. ~Thanks Dave.










Categories
Archives

Coming Soon: Affiliate Traffic Generator | Affiliate Directory | Affiliate Software | Affiliate Tools

Resdaz Media
AffiliateBestPrograms © 2007-20009 Resdaz Media
All Rights Reserved
Other Resdaz Media Network Sites:
Add to Technorati Favorites

BRDTracker BlogsByCategory.com
Find the best blogs at Blogs.com. feeds4all

Blogoculars Blog Directory