$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display Random Posts with thumbnail instead of just a title of the post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display Random Posts with thumbnail instead of just a title of the post

matteradmin8PV0评论

This code works really well. It displays the title of the post randomly with permalink but my objective is to display the thumbnail and/or featured image instead.

Any help would be appreciated.

<?php function wpb_rand_posts() { 
    $args = array(
    'post_type' => 'post',
    'orderby'   => 'rand',
    'posts_per_page' => 5, 


    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
    $string = '<ul>';
        while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .' </a></li>';

        }
        $string .= '</ul>';

    /* Restore original Post Data */
    wp_reset_postdata();
    } else {
    $string .= 'no posts found';
    }
    return $string; 
} 
add_shortcode('random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');  ?>

This code works really well. It displays the title of the post randomly with permalink but my objective is to display the thumbnail and/or featured image instead.

Any help would be appreciated.

<?php function wpb_rand_posts() { 
    $args = array(
    'post_type' => 'post',
    'orderby'   => 'rand',
    'posts_per_page' => 5, 


    );
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
    $string = '<ul>';
        while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .' </a></li>';

        }
        $string .= '</ul>';

    /* Restore original Post Data */
    wp_reset_postdata();
    } else {
    $string .= 'no posts found';
    }
    return $string; 
} 
add_shortcode('random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');  ?>
Share Improve this question edited Feb 15, 2019 at 14:27 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Feb 15, 2019 at 13:56 gafmgafm 1011 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

You can get the post thumbnail HTML using the function get_the_post_thumbnail(), so you just need to replace get_the_title() with that function:

$string .= '<li><a href="'. get_permalink() .'">'. get_the_post_thumbnail() .' </a></li>';

By default the image will be the size defined by your theme, but you use any of the other available sizes by passing the size name as the 2nd argument of the function:

$string .= '<li><a href="'. get_permalink() .'">'. get_the_post_thumbnail( null, 'large' ) .' </a></li>';

Replace this line

$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .' </a></li>';

WITH

$string .= '<li><a href="'. get_permalink() .'">'. get_the_post_thumbnail( get_the_ID(), 'thumbnail' ) .' </a></li>';

get_the_post_thumbnail function is showing the featured image at front end.

Here is the full code:

<?php 

function wpb_rand_posts() { 
    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 5
    );

    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) {
        $string = '<ul class="wpb-rand-posts">';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            $string .= '<li><a href="'. get_permalink() .'">'. get_the_post_thumbnail( get_the_ID(), 'thumbnail' ) .' </a></li>';
        }

        $string .= '</ul>';

        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        $string .= 'no posts found';
    }

    return $string; 
} 
add_shortcode('random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode');  

?>

I added the CSS class name into the UL tag.

You will add this CSS into your style.css file

ul.wpb-rand-posts {
    list-style-type: none;
    margin: 20px 0;
    padding: 0;
}

.wpb-rand-posts li {
    display: inline-block;
    float: left;
    margin-left: 10px;
    width: calc( ( 100% - 40px ) / 5 );
}

.wpb-rand-posts li:first-child {
    margin-left: 0;
}
Post a comment

comment list (0)

  1. No comments so far