How to Create a Popular Posts Page For Your WordPress Blog

Adding a popular posts page in your blog is a great way to hook the first time readers and show them what other readers love to read on your blog.  Web users have a short attention span and when they visit your blog, they always want to read your best content as quickly as possible.

There are quite a few ways to show a list of popular posts in WordPress – show popular posts based on comment count, show popular posts based on pageviews or show popular posts manually using a tag or category.

1. Show Popular Posts Based on Comment Count:

This method displays the most commented posts as an ordered list, just paste the following code where you want the list of popular posts to appear in your theme:

<ul>
<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5″);
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href=”<?php echo get_permalink($postid); ?>” title=”<?php echo $title ?>”>
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

To change the number of posts shown, replace 5 in line number 3 with a custom number. This method does not require any plugin but I think that the most commented posts need not necessarily be the most popular posts of your blog.

Display Popular Posts Using a WordPress Plugin

This method uses WordPress.com stats to know the approximate pageviews of every single post of your blog. Then it uses a WordPress plugin to display the posts in your theme.

This method is more meaningful, since the users can see and read those articles which gets the maximum number of pageviews

1. Install the WordPress.com Popular Posts plugin. You will also need the WordPress.com stats plugin for the popular post plugin to work.

2. You have to first create a page template where you can paste the php code which will show the list of popular posts. Creating a page template is very easy, just open notepad and paste the following code:

<?php
/*
Template Name: popular
*/
?><?php get_header(); ?>
<?php WPPP_show_popular_posts( “title=&number=90&days=90&format=<a href=’%post_permalink%’ title=’%post_title_attribute%’>%post_title%</a>”);?>
<?php get_footer(); ?>

Tweaking the above code:

There are quite a few things to note regarding the above code:

  • If you want to modify the number of articles shown in the list of popular posts, edit &number=90 and enter the number of articles you want to show.
  • To edit the number of days in consideration, edit &days=90 and enter the number of days for which the stats will be considered.

You will get the full list of commands in the plugin project page.

3. Save the file as popular.php and upload it in the same directory of your current theme. Remember to save as “All files” and not as a text document.

4. Log in to WordPress administration area and create a new page. Leave the content of the page blank and pick the popular posts template which you created in step 2.

Pick a page template

Hit publish and you are done. The popular posts page is live in your blog and it will automatically list those posts which gets the maximum pageviews in the defined amount of time (days) .

5. To show the popular posts in the sidebar or footer of your theme, use the following code

<?php WPPP_show_popular_posts( “title=&number=90&days=90&format=<a href=’%post_permalink%’ title=’%post_title_attribute%’>%post_title%</a>”);?>

6. To show popular posts as widget, go to Appearance > Widgets and drag and drop the popular posts widget in the available widget sections of your theme.

You can specify the number of days, number of links, length of title and configure almost every parameter from the widget options. If you want to show popular posts from a particular category, select the desired category from “Only Show posts in this category” drop down.

Note: WordPress do not allows executing PHP code from the pages, hence we used a custom template. You can use a WordPress page and the PHP exec WordPress plugin to execute the popular posts code as described.

3. Display Popular Posts Manually From a Particular Tag or Category

This method is simple. You have to create a tag or category as “popular” and manually assign the posts which you want to appear in the Popular posts section. Just follow these steps:

1. Create a unique tag in your blog as “popular”. You will use this tag to manually tag selected posts as “popular” and then display a list of posts from that tag.

2. Open notepad or any HTML editor and paste the following code:

<?php
/*
Template Name: popular
*/
?>
<?php get_header(); ?>

<h1>Popular Posts</h1>

<?php query_posts(‘tag=popular’); ?> //tag used to fetch posts
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div id=”post-<?php the_ID(); ?>”>
<a href=”<?php the_permalink() ?>” rel=”bookmark”><?php the_title(); ?></a>
<div><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php else : ?><p>Sorry, but you are looking for something that isn’t here.</p>
<?php endif; ?>
<?php get_footer(); ?>

3. Save the file as popular.php and upload to the same directory of your WordPress theme.

4. Log in to WordPress administration area, create a new page and select the page template as “popular”. Hit publish and you are done. The popular posts page is live on your blog.

Whenever you want to manually add a post in the popular posts page, simply add the tag “popular” to that post.

Tip: How to Create a Featured section in WordPress blog

Related articles

How To Disable Chrome Incognito Mode On Windows And Mac

How To Watch Netflix With Friends From Remote Distance

How To Mirror Or Flip Photos On iPhone And iPad

Fix Instagram Keeps Crashing Or Not Working Issue

Leave a Reply

Your email address will not be published. Required fields are marked *