I wanted to write a query to display, 3-4 top popular post, in my sidebar with a small thumbnail size image of the post. I tried looking at archives widget, but it only displayed links. (thought, i could copy the code from there), how do i get to have my desired result?
1 Answer
You can attach images to each post using "featured images" by first enabling featured images for your theme using add_theme_support('post-thumbnails'), and get_the_post_thumbnail() to display images.
For example, to list 4 most commented posts with their featured image and title, you can do something like this:
$posts = get_posts(
array(
'posts_per_page' => 4,
'order_by' => 'comment_count'
)
);
foreach ($posts as $post) {
get_the_post_thumbnail($post->ID)
echo $post->post_title;
}
Read about WP_Query for more querying options. To get more advanced measurements, you may want to look into a plugin.
-
but its simple query, i want to display most popular posts and archive displays? Dec 14, 2013 at 8:10
-
The example shows how to display post thumbnails with your posts, to get popular posts you have to add some criteria to the get_posts() call. You can for example order by comment count using
'orderby' => 'comment_count'
. It really depends on which measurement you use to determine if a post is popular– stianlikDec 14, 2013 at 10:05 -
@stanlik now i have this pastebin.aquilenet.fr/… and the date is just displaying for first post Dec 14, 2013 at 11:41