Last active
June 4, 2016 15:38
-
-
Save Atala/2a3e8b910e49557b65af920133e5e2fa to your computer and use it in GitHub Desktop.
Add filter example
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
<?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