WordPress – Most Viewed – Sidebar Widget

August 20th, 2007 by Stephen Cronin (Please wait) [Shortlink]

I use Lester Chan’s WP-PostViews plugin to count how many views each post gets. I recently added a Most Viewed section to my Sidebar. The problem? I use WordPress Widgets and there is no widget for the plugin.

Around the time this post was written, Lester released a new version of the WP-PostViews plugin including support for widgets. There is no longer any need for the work-around explained in this post.

This post assumes you are using the WP-PostViews plugin and WordPress Widgets. If you are not, then this post is not for you.

Setting It Up

Fortunately, it is fairly simple to get this to work. First you need to get Otto’s ExecPHP plugin. This allows you have a ‘textbox’ widget which can execute php code.

Once you’ve installed and activated this plugin, go to the Presentation – Widgets (or Sidebar Widgets) page. You should now have the PHP Code 1 widget available (you can add more if you need them).

Drag the PHP Code 1 widget to wherever you’d like it to appear, then click on the configure icon on the right hand side of the widget. A dialog box will open. Enter an appropriate title, such as Most Viewed and put the following in the main section:

<ul><?php if (function_exists('get_most_viewed')): ?>
<?php get_most_viewed(); ?>
<?php endif; ?>
</ul>

Make sure you add the <ul> and </ul> before and after the php code. I originally left this out and found that the list was not formatted properly (because it wasn’t a list). I eventually found the solution from Otto himself on a WordPress Support page.

Close the dialog, click Save Changes and you should now have your Most Viewed section on your sidebar.

Changing The Default Behaviour

By default, the WP-PostViews plugin will display the top 10 posts and pages. It’s possible to change this by passing parameters in the get_most_viewed(); call. This is documented by Lester Chan on the plugin page, but here is a quick rundown:

The first parameter indicates what type you want: 'post' for posts, 'page' for pages, or 'both' (which is the default behaviour). The second parameter indicates how many results you want returned.

For example, I decided I only want 6 items (posts and pages) listed, so I replaced:

<?php get_most_viewed(); ?>

with

<?php get_most_viewed('both', 6); ?>

Final Word

Having a Most Viewed section on your sidebar is a great way of keeping visitors on your site. If someone lands on one of your pages, they can quickly see your most popular posts, which are the ones most likely to entice them to browse your site further.

For someone with a content (rather than date) based site, such as myself, this is much more important than the monthly archive section.

If you use a different method to display popular posts, or have any other tips about keeping visitors on your site, please let me know.

Like this article? Plus One it!

Tags: , ,

There are 17 reponses to this article. Visit the comments page.

Leave a Reply

Anti-Spam Quiz:


Stephen Cronin

is Manager of Online Service Delivery at a Queensland Government department & has been a freelance WordPress developer/consultant since 2007
*Content on this site is my own and is not related to my employer

Hire me - I'm expensive, but I'm very good!

WordPress Plugins

Want a Custom WordPress plugin? See my Services page.

Greasemonkey Scripts

Visit my home page at Userscripts.org.