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 />
foreach ( $searches as $searchz ){ echo '<tr><td style="border-top:#A27C08 1px solid;"><br /> |
* 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 */ $searches = $wpdb->get_results($sql); echo '<table border="0" cellpadding="0" cellspacing="1" width="500"><br /> foreach ( $searches as $searchz ){ echo "</table><br /><br />"; ?> |
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 !!!
26 Responses to 'Wordpress Hack : Most Popular Articles'
Leave a Reply
















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 ?
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.
it doesnt worked for me :S
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
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.
This is exactly what I expected to find out after reading the title ress Hack : Most Popular Articles. Thanks for informative article
[…] can find the code at his site here Did You Enjoy this Post? Subscribe to Dollars […]
Awesome post. Thanks for this. I have a few blogs on WP and can’t wait to try this hack on them.
:)
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!!
Hello! Good Site! Thanks for the hack. I’ve already added it to my blog and it works great.
This is exactly what I expected to find out after reading the title ress Hack : Most Popular Articles. Thanks for informative article
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”. ;)
Golly. I feel so fortunate to have found this. Bonus! Thanks for the great work. Are you married? =)
[…] here for more No Comments so far Leave a comment RSS feed for comments on this post. TrackBack […]
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….
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.
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
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
I am using widgets also and I put this hackon my blog and everything is working fine. No problems.
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.
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!
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.
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.
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.
nice hack :)
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!