• domdeez

    (@domdeez)


    I would like to add an “IF” statement to the function below. Currently it grabs the first image from a post. If there is no first image it will display a default image.

    I would like to change it so that it (1.) grabs the first image from a post, (2.) if there is no image in the post, then it grabs the image from the featured image, custom field, or an attached image from that posts gallery. It doesn’t have to be all three options just one, the easiest to implement. (3.) then if there is no image available from #2 then it displays the default.

    My functions.php

    // Get URL of first image in a post
    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];
    
    // no image found display default image instead
    if(empty($first_img)){
    $first_img = "/path/default.png";
    }
    return $first_img;
    }

    And I call it with this

    <div id="category-thumblist">
    	<div class="thumb"><a href="<?php the_permalink() ?>"><img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo catch_that_image() ?>&w=100&zc=1&h=100" alt="<?php the_title(); ?>"/></a>
    	</div>
    	<div class="excerpt"><a class="title_link" href="<?php the_permalink() ?>"><?php the_title() ?></a><br><?php the_excerpt() ?>  <a href="<?php the_permalink() ?>">read more</a></div><hr />
    
    </div>

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Catch the Image with an extra "IF" statement’ is closed to new replies.