-
-
Save norcross/3064651 to your computer and use it in GitHub Desktop.
images with fallbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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