How to Create a Most Popular Posts page in WordPress
↳ Post created Saturday, March 19th 2011 by admin. ⌈no comments⌋This tutorial will help you to create a page with top 10 most popular by views posts in WordPress. To do this you need to create the sort by injection.
First you need to create a popular.php file and paste the following code:
<?php
/*
Template Name: Most Popular
*/
?>
<?php get_header(); ?>
<h2><?php the_title(); ?></h2>
<?php
$i = 1;
$orderby="meta_value_num";
$order="DESC";
// create the sort by injection
$posts = query_posts('meta_key=hits&orderby='.$orderby.'&order='.$order.'');
if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php include("_popular.php"); ?>
<?php $i++; unset($alt); ?>
<?php endwhile; endif; ?>
<?php get_sidebar();?>
<?php get_footer(); ?>
Now create another file _popular.php and add the following piece of code:
<h1><a href="<?php echo get_permalink($post->ID)?>" title="<?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a></h1> <?php echo $post->post_excerpt; ?>
In the end last step is setting up the page. To do this go to your WordPress dashboard, and click Pages > Add New. Create a new page with title “Most Popular” and then in the “Page Attributes” window, set the “Template” to “Most Popular”.