• maxxis

    (@maxxis)


    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

Viewing 1 replies (of 1 total)
  • Thread Starter maxxis

    (@maxxis)

    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;” >‘; } ?>

    <?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.