Skip to content

Instantly share code, notes, and snippets.

@Atala
Last active June 4, 2016 15:38
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 Atala/2a3e8b910e49557b65af920133e5e2fa to your computer and use it in GitHub Desktop.
Save Atala/2a3e8b910e49557b65af920133e5e2fa to your computer and use it in GitHub Desktop.
Add filter example
<?php
add_filter('post_thumbnail_html', 'get_first_image_if_no_thumb');
function get_first_image_if_no_thumb($html, $post_id, $post_image_id) {
if ($html == '') {
$post = get_post($post_id);
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches[1][0];
if (isset($first_img)) {
return '<img width="300" class="attachment-medium size-medium wp-post-image" src='. $first_img .'></img>'
}
}
return $html;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment