1

Hi guys wondering if anyone can help. I have this code that lists all sub pages, their featured image and title/link. What I'd like to be able to do is randomize the the size of the featured image between what Wordpress offers... small/medium/large?

Any ideas?

    <?php 
    $args = array(
    'parent' => 2,
    'post_type' => 'page',
    'post_status' => 'publish'
    ); 
$pages = get_pages($args);  ?>
<ul> 
<?php
foreach( $pages as $page ) {
?>

 <li>
    <a href="<?php echo  get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
    <div><span class="thumbnail"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></span>
    <span class="title"><?php echo $page->post_title; ?></span></div>
    </a>
</li>
<?php
 }  
?>
    </ul>
1

You can put the sizes into an array and use PHP's array_rand to choose a random size:

$sizes = array( 'small', 'medium', 'large' );

foreach( $pages as $page ){
    $random_size = array_rand( $sizes, 1 );
    echo get_the_post_thumbnail( $page->ID, $sizes[$random_size] );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.