Created
November 18, 2015 14:35
-
-
Save sharkyak/beb21a437627a2a60a74 to your computer and use it in GitHub Desktop.
wordpress get image form post
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
| function get_post_image() { | |
| global $post, $posts; | |
| $first_img = ''; | |
| ob_start(); | |
| ob_end_clean(); | |
| $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); | |
| $first_img = $matches [1] [0]; | |
| if(empty($first_img)){ | |
| $img_dir = get_bloginfo('template_directory'); | |
| $first_img = $img_dir . '/images/pf1.jpg'; | |
| } | |
| return $first_img; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment