WordPress: Display list of random posts

Geschätzte Lesezeit: 2 Minuten, 14 Sekunden

Many blogs display related posts for an article. By adding a list of random posts below your blog post, the link profile of your site can be improved. You even do not need a plugin for that!

random post photo
Photo by Bods

Benefits of random posts

There are always some texts that do not correlate with the subject of other articles. That is the reason why they are not interlinked via similar or related content. A popular plugin that is there to build up a list of related posts for the current article is called YARPP (Yet Another Related Posts Plugin). Furthermore, having links from the top posts on your site helps only some specific referenced pages, but not the others.

In order to pull virtually any blog post into the game and into the eye of the reader, you have to enhance WordPress with some code. I for myself was unable finding a current and fully functional plugin for that purpose so I decided to go for a faster and hand-made solution.

Display random posts as list

In this very blog you can see how it looks like by scrolling down to the end of this post (in German):

Random Posts
Showing related content

This list is generated randomly and automatically for any WordPress posting. No need for a plugin which would slow down your WordPress site. You can even use the following solution in the widget area, so that the list of posts can be displayed in a sidebar, e.g.

And that’s how it works  (demonstrated on the Theme Twentythirteen, for other themes it may be necessary to do some little adaptions on the code or choose a different PHP file):

First of all, open the file named content.php via the menu  Design -> Editor and add the following lines of source code at the end of the file. But please care that the code resised within the article-Tag  (meaning </article> follows afterwards):

<h2>Further posts</h2>
<?php
$args = array( 'posts_per_page' => 4, 'orderby' => 'rand','post_type' => 'post' );
$rand_posts = get_posts( $args );
echo "<ul>";
foreach ( $rand_posts as $post ) {
?>
<li><a href="<?php echo $post->guid; ?>"><?php echo $post->post_title; ?></a></li>
<?php }
echo "</ul>";
wp_reset_postdata();
?>

With this code in place, four random posts will be shown below the current post text. Each of these random entries contains the title as well as the permalink. The whole list is titled with a H2 text Further Posts, which youl can change to what you want as well as the number of the list entries. it would be even possible to show thumbnails for the random entries, but that may be getting a little complicated.

It is important to notice that the last command is wp_reset_postdata, because if you skip it, all further selections of posts on the current page will be under the influence of randomness. In case links are displayed below the content in chronological order (like previous and next post), that logic would get screwed up.


How did you like this post?

WordPress: Display list of random posts: 1 Stern2 Sterne3 Sterne4 Sterne5 Sterne 5,00 von 5 Punkten, basieren auf 4 abgegebenen Stimmen.

Webhosting inkl. Homepagebaukasten!
Dr. Klaus Meffert
Folge mir

Dr. Klaus Meffert

Der Autor ist Diplom-Informatiker und hat neben seinem Beruf als Organisationsberater zum Doktoringenieur promoviert. Er besitzt 30 Jahre Erfahrung im IT-Bereich (Software-Lösungen, Apps, Web, Online-Marketing, IT-Beratung) ist mehrfacher Buchautor und Autor zahlreicher Fachartikel. Er hilft Unternehmen mit maßgeschneiderten Software-Lösungen und mit besseren Google-Rankings zu mehr Erfolg.
Dr. Klaus Meffert
Folge mir

Weitere Beiträge

Schreibe einen Kommentar

Als Name kann auch ein Pseudonym angegeben werden. Deine Email-Adresse wird nicht veröffentlicht. Siehe unsere Datenschutzbestimmungen