Last updated on September 25, 2023

Display a Random Post Thumbnail

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Display random post thumbnails.

To display a random post thumbnail in WordPress, you can use the get_posts() function to retrieve a random post, and then use the get_the_post_thumbnail() function to display its thumbnail. Here’s an example code snippet that achieves this:

<?php
// Get a random post
$random_post = get_posts( array(
    'orderby'        => 'rand',
    'posts_per_page' => 1,
) );

// Check if a random post is found
if ( $random_post ) {
    // Get the post thumbnail
    $thumbnail = get_the_post_thumbnail( $random_post[0]->ID, 'thumbnail' );

    // Display the post thumbnail
    echo $thumbnail;
}
?>

This code snippet retrieves a random post using the get_posts() function with the 'orderby' => 'rand' parameter. It limits the result to one post using the 'posts_per_page' => 1 parameter. Then, it checks if a random post is found and retrieves its thumbnail using the get_the_post_thumbnail() function. Finally, it displays the post thumbnail using echo.

You can place this code snippet in your theme files, such as single.php or index.php, to display a random post thumbnail wherever you want it to appear. This can be useful for creating a “Random Post” section on your website or adding visual interest to your sidebar.

Examples

Example 1: Display a random post thumbnail on the homepage

This use case demonstrates how to display a random post thumbnail on the homepage of a WordPress website. The code example uses the wpsnippets_get_random_post_thumbnail() function to retrieve a random post thumbnail and then displays it using the the_post_thumbnail() function.

<?php
function wpsnippets_get_random_post_thumbnail() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'orderby' => 'rand',
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS',
            ),
        ),
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $query->the_post();
        $thumbnail = get_the_post_thumbnail();
        wp_reset_postdata();
        return $thumbnail;
    }

    return '';
}
?>

<?php echo wpsnippets_get_random_post_thumbnail(); ?>

The wpsnippets_get_random_post_thumbnail() function uses WP_Query to retrieve a random post that has a featured image. It then uses get_the_post_thumbnail() to get the post thumbnail and returns it. The thumbnail is displayed on the homepage using echo.

Example 2: Display a random post thumbnail in a sidebar widget

This use case demonstrates how to display a random post thumbnail in a sidebar widget. The code example uses the wpsnippets_get_random_post_thumbnail() function to retrieve a random post thumbnail and then displays it using the the_post_thumbnail() function.

<?php
function wpsnippets_get_random_post_thumbnail() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'orderby' => 'rand',
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS',
            ),
        ),
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $query->the_post();
        $thumbnail = get_the_post_thumbnail();
        wp_reset_postdata();
        return $thumbnail;
    }

    return '';
}
?>

<div class="sidebar-widget">
    <?php echo wpsnippets_get_random_post_thumbnail(); ?>
</div>

The wpsnippets_get_random_post_thumbnail() function is used to retrieve a random post thumbnail. The thumbnail is then displayed within a <div> element with the class sidebar-widget.

Example 3: Display a random post thumbnail in a custom template

This use case demonstrates how to display a random post thumbnail in a custom template. The code example uses the wpsnippets_get_random_post_thumbnail() function to retrieve a random post thumbnail and then displays it using the the_post_thumbnail() function.

<?php
function wpsnippets_get_random_post_thumbnail() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'orderby' => 'rand',
        'meta_query' => array(
            array(
                'key' => '_thumbnail_id',
                'compare' => 'EXISTS',
            ),
        ),
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $query->the_post();
        $thumbnail = get_the_post_thumbnail();
        wp_reset_postdata();
        return $thumbnail;
    }

    return '';
}
?>

<div class="custom-template">
    <?php echo wpsnippets_get_random_post_thumbnail(); ?>
</div>

The wpsnippets_get_random_post_thumbnail() function is used to retrieve a random post thumbnail. The thumbnail is then displayed within a <div> element with the class custom-template. This code can be used in a custom template file to display a random post thumbnail in a specific location.

Last updated on September 25, 2023. Originally posted on October 6, 2023.

Leave a Reply