Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'the_content' , 'mh_get_first_img' );
- function mh_get_first_img( $content ) {
- $mh_url_regex = "/<img\ .*\/\>/";
- // Get all the matches and populate them in an array
- preg_match_all( $mh_url_regex , $content, $mh_matches );
- if ( $mh_matches[0][0] ) {
- // Zap everything after the first closing '>'
- $mh_first_img = preg_replace( '/>.*$/' , '>', $mh_matches[0][0]);
- // Add the img HTML to the top of the post as an example.
- $content = '<p><pre>' . esc_html( $mh_first_img ) . '</pre></p>' . $content;
- }
- return $content;
- }
Advertisement
Add Comment
Please, Sign In to add comment