Skip to content

Instantly share code, notes, and snippets.

@norcross
Forked from eddiemachado-zz/Image-Waterfall.php
Created July 7, 2012 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/3064651 to your computer and use it in GitHub Desktop.
Save norcross/3064651 to your computer and use it in GitHub Desktop.
images with fallbacks
// let's go through and get the right image
function dg_thumb_waterfall() {
global $post;
if ( has_post_thumbnail($post->ID) ) {
echo '<div class="thumbnail-wrapper">';
the_post_thumbnail('digi-med-thumb');
echo '</div>';
} else {
// get the category for the post and show the image
$category = get_the_category($post->ID);
$cat_slug = $category[0]->slug;
$cat_name = $category[0]->cat_name;
// let's grab the first image in the post
$content = $post->post_content;
$output = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
if(!empty($matches))
$first_img = $matches [1] [0];
// if there's no first post, lets use a default
if(empty($matches)) {
$img_fb_sc = get_bloginfo('stylesheet_directory').'/library/images/'.$cat_slug.'.jpg';
$first_img = '<div class="thumbnail-wrapper"><img width="200" height="150" class="attachment-digi-med-thumb wp-post-image no-thumb-fallback" src="'.$img_fb_sc.'" alt="' . $cat_name . '" /></div>';
}
// display what we've got
return $first_img;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment