How to display the most popular articles with featured images


In this post, we will explain how to display the most popular articles, which depends mainly on the number of comments for articles, meaning that the more comments in the article, the more popular it is. This is the concept on which the explanation is based.Explanation for beginners in the development of WordPress templates. For those who have no skill in this field, all you have to do is use one of the suggested additions at the bottom of the post

Fetch the most popular articles

Of course, we will need a hack or a function in the literal sense to fetch the most commented articles from the database. All you have to do here is add the code below to the functions.php . file

1<?php
2/*
3* Get Most Posts Commented ID
5* @Mouad Achemli
6*/
7function get_post_commented($post_id = NULL) {
8global $wpdb;
9$date_lookup = ”;
10$table = $wpdb->prefix . “comments”;
11
13if ( NULL === $post_id )
14$post_id = get_the_ID();
15
16// Get list of commented posts in the current mounth
17$sql = “SELECT comment_approved, comment_post_ID, comment_author, comment_author_email, comment_date, comment_content, comment_approved, comment_type, comment_parent
18FROM ” . $table . ” WHERE comment_date > ‘%$date_lookup%’ AND comment_date >= ‘”.date(‘Y-m-d’, strtotime(date(‘Y-m-01 00:00:00′))).”‘ ORDER BY comment_date DESC”;
19
20$post_ids = $wpdb->get_results($sql);
21$output = array();
22
23foreach ($post_ids as $id) {
24array_push($output,$id->comment_post_ID);
25}
26
27$result = array_unique($output);
28return $result;
29}
30?>

Featured Image Display Requirements

You must make sure that the template you are using supports thumbnails. There are two ways to use it:

  1. Using the Featured Images Feature in WordPress 2.9
  2. How to display featured image by TimThumb

In this case, the difference will be only in the way images are used in relation to displaying the most popular articles

Displaying images using the WordPress feature

As for the display by the prominent image feature, we paste the following code in the sidebar (sidebar.php) anywhere you want the most popular topics to appear:

1<?php
2$postscount = 5;
3$param = array (
4‘post__in’ => get_post_commented($post_id = NULL),
5‘orderby’ => ‘comment_count’,
6‘posts_per_page’ => $postscount
7);
8$most_popular = new WP_Query($param);
9echo ‘<ul>’;
10while ($most_popular->have_posts()) : $most_popular->the_post();
11?>
12<li>
13<h3><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h3>
14<?php if ( has_post_thumbnail() ) : ?>
15<div class=”thumbnail”>
16<?php the_post_thumbnail(‘thumbnail’); ?>
17</div>
18<?php endif; ?>
19</li>
20<?php
21endwhile;
22wp_reset_query();
23echo ‘</ul>’;
24?>

Here it will display five articles. If you want to display more articles, change the value of the $postscount variable from the code. As for the HTML tags, you can change them according to what you see fit for the shape of your site

View photos by timthumb

For the view using the timthumb script, use the code below

1<?php
2$postscount = 5;
3$param = array (
4‘post__in’ => get_post_commented($post_id = NULL),
5‘orderby’ => ‘comment_count’,
6‘posts_per_page’ => $postscount
7);
8$most_popular = new WP_Query($param);
9echo ‘<ul>’;
10while ($most_popular->have_posts()) : $most_popular->the_post();
11?>
12<li>
13<h3><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h3>
14<div class=”thumbnail”>
15<?php show_thumb(80, 50, ‘img-responsive’, $post->ID, 100); ?>
16</div>
17</li>
18<?php
19endwhile;
20wp_reset_query();
21echo ‘</ul>’;
22?>

Here it will display five articles. If you want to display more articles, change the value of the $postscount variable from the code. As for the HTML tags, you can change them according to what you see fit for the shape of your site

Additions – Most Popular Articles

This is the list of plugins that you can use to display the most popular articles in WordPress

  1. Popular Widget
  2. Popularity Posts Widget
  3. Top 10 – Popular posts plugin for WordPress
  4. WordPress Popular Posts

If you have any questions, please leave a comment and don’t forget to subscribe to our mailing list. Thank you very much

Peace, mercy and blessings of God

Leave a Comment