Jul 3 2008

How to create a popular posts page with the wp-postviews plugin

If you're new here, you may want to start with my most popular posts. Then, subscribe to my RSS feed to stay updated. Thanks for visiting!

Searchmarked.com Tip

I get a lot of questions on how I created my Popular page on Searchmarked.com. I use a combination of Lester Chan’s wp-postviews plugin and some thinking outside the box (literally) to accomplish this.

This recipe requires that plugin to work so follow the steps below to get started:

-Download wp-postviews

-Install wp-postviews on your Wordpress blog

-Read the usage instructions

-I used the below scenario for my page

From the readme:

  • To Display Most Viewed Post
  • Use:

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

The first value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.

The second value you pass in is the maximum number of post you want to get.

Default: get_most_viewed(’both’, 10);

-Now the trick is to actually get this code on the page. As you probably know, you can’t put php code straight into a wordpress page or blog post. This is where you have to think outside the box (the text box in this case).

-You need to create a new template and assign it to the page. The template will contain everything that needs to show up on the page, including the blog skin itself. Nothing will actually be written in the page itself (hence thinking outside the box).

-To create your own page template all you need to do is use the following code at the top of your php file:

Template Name: Popular

-The rest is just normal php and html.

-Our template will use the code from /wp-content/themes/YourTheme/page.php as its base.

-Copy and paste the code from /wp-content/themes/YourTheme/page.php into a new file named popular.php if you wish.

-Make room at the top for the following code to make popular.php into a page template:

Template Name: Popular

-Look for a line similar to the following:

<?php the_content(); ?>

-Place the code for your wp-postviews page under this line.

-For example, here is the code for my Popular page:

<h2>Below are the top 10 posts on Searchmarked.com.</h2>
<?php if (function_exists('get_most_viewed')): ?>
<?php get_most_viewed(); ?>
<?php endif; ?>

-Now save this file as popular.php or whatever you want to name it.

-Upload it to your /wp-content/themes/YourTheme/ directory.

-Now create a new page for your popular posts and name it whatever you like (I named mine popular).

-Leave the page blank and choose the page template that you just uploaded for it.

-Now publish the page with the new “popular.php” template and you’re all done.

That’s it! You should now have a popular posts page. Happy Posting!

Technorati Tags: , , ,

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

3 Comments on this post


  1. Harrison McLeod said:

    Hey, just installed the plugin and followed your directions but I’m getting a fatal error. Any clue for a fix on this?

    July 19th, 2008 at 1:14 pm
  2. hstagner said:
    July 19th, 2008 at 4:11 pm
  3. oyun said:

    I’m getting a fatal error. Any clue for a fix on this?

    January 25th, 2010 at 2:35 pm


Subscribe Form

Subscribe to Blog


Recent Readers

                  Computers Blogs - Blog Top Sites