Title: Help please ! &#8211; Modifying the &quot;catch_that_image()&quot; function.
Last modified: August 19, 2016

---

# Help please ! – Modifying the "catch_that_image()" function.

 *  [maxxis](https://wordpress.org/support/users/maxxis/)
 * (@maxxis)
 * [15 years ago](https://wordpress.org/support/topic/help-please-modifying-the-catch_that_image-function/)
 * Hello folks, I am a newbie to coding and PHP, but I am attempting to build a 
   WordPress theme for a personal blog and need some help from the WP / PHP experts.
 * I am using the “catch_that_image()” function to pull out the first image of every
   post and display it as a Thumbnail (through Timthumb)
 * I am trying to figure out a way in which the function can be modified to pull
   out a random image from every post. [Instead of the first post, it should pull
   out a random image]
 * Since most of my posts will have atleast 25 images each, it would be great if
   the function can pull out a random image from these 25 to use as a thumbnail.
 * If an occasional post has less than 25 images and the function is unable to find
   the random image it is looking for, it should just show the first image as a 
   default.
 * Please help me out 🙂
 * The “catch_that_image()” discussion is on this link:
    [http://wordpress.org/support/topic/retreive-first-image-from-post](http://wordpress.org/support/topic/retreive-first-image-from-post)

Viewing 1 replies (of 1 total)

 *  Thread Starter [maxxis](https://wordpress.org/support/users/maxxis/)
 * (@maxxis)
 * [15 years ago](https://wordpress.org/support/topic/help-please-modifying-the-catch_that_image-function/#post-2043620)
 * Please please please help…. 🙁
 * This is the function:
    ——————————-
 * function catch_that_image() {
    global $post, $posts; $first_img = ”; ob_start();
   ob_end_clean(); $output = preg_match_all(‘/<img.+src=[\'”]([^\'”]+)[\'”].*>/i’,
   $post->post_content, $matches); $first_img = $matches [1] [0];
 *  if(empty($first_img)){ //Defines a default image
    $first_img = “images/default.
   gif”; } return $first_img; }
 * And this is how I am calling it….
    —————————————– <?php if(catch_that_image()){
   echo ‘[<img src=”‘ .get_stylesheet_directory_uri(). ‘/timthumb.php?src=’ .urlencode(catch_that_image()). ‘&h=63&w=84&zc=1&q=90″ style=”float: left; padding:10px 9px 0px 5px;” >](https://wordpress.org/support/topic/help-please-modifying-the-catch_that_image-function/&apos; .get_permalink(). &apos;?output_format=md)‘;}?
   >
 * <?php the_excerpt(‘Read on…;’); ?>
 * </div>
    <span class=”btn-readon”>” rel=”bookmark” title=”<?php the_title(); ?
   >”>Read On
   </span>
 *  </div>
 * <?php endwhile; ?>
 * <br clear=”all” />
 * <?php else: ?>
 *  <!– Error message when no post published –>
 * <?php endif; ?>
 * ***********************************************************
 * Instead of the first image from every post, I just need the function to pull 
   up a random image from every post… WP / PHP experts please help me. I have already
   spent two full days trying to do something, and nothing worked.

Viewing 1 replies (of 1 total)

The topic ‘Help please ! – Modifying the "catch_that_image()" function.’ is closed
to new replies.

## Tags

 * [catch_that_image](https://wordpress.org/support/topic-tag/catch_that_image/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [maxxis](https://wordpress.org/support/users/maxxis/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/help-please-modifying-the-catch_that_image-function/#post-2043620)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
