Create a popular posts shortcode in WordPress

maxime lebrun 698791 unsplash

This is a useful way to display popular posts on your blog to encourage users to read more articles rather than just bounce away.

The purpose of this shortcode is to display the three most popular posts on your blog. Popularity in this case is measured by the number of comments. I felt that comments were a better indicator of engagement than simple page views.

Note that the query gets the four most popular posts – just in case the current post is popular.

Rather than running the query every time the page loads, the query results are stored in a transient which is updated weekly. The code is being used on this site as part of the Showcase theme so some of the class names are specific to that theme.

The shortcode outputs the featured image, title and a link to the article. It shouldn’t take too much work to add the excerpt or any other content that you might want.

Here’s the code in its entirety:

And just take a look below to see it in action:

Leave a Reply

Your email address will not be published. All fields are required.