Get a first image of a post with preg_match_all
-
Hi there,
I would need a help with preg_match_all function. I try to get first image of a post for related posts plugin. Currently I use following function:
function get_first_image() { global $post, $posts; $first_img = ''; $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $post->post_content, $matches); $first_img = $matches [1][0]; return $first_img; }
This function is working without a problem. The problem is that some posts are not in the wp_posts table so it doesn’t show any picture for some items. Some posts contents are in wp_post_settings table created by a plugin where new posts contents are saved. How should the function looks now to work for both (for classic post content in the wp_posts as well as for wp_post_settings.
Thanks.
- The topic ‘Get a first image of a post with preg_match_all’ is closed to new replies.