• Hey,

    I am trying to get the image thumb added in the post at the time of adding the post.

    I am using the_post_thumbnail(‘thumbnail’) function however it is showing the featured image. I would like to get the image from the content. How can i do this with WordPress default functions.

    Please help

    Thanks
    Zack

Viewing 1 replies (of 1 total)
  • I don’t think there is a WordPress function for that but I use this function to get the first image from the post content:

    function get_first_image() {
        global $post;
        $first_image = '';
        ob_start();
        ob_end_clean();
        $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
        $first_image = $matches[1][0];
    
        if(empty($first_image)){ //return false if no image is found
            $first_image = false;
        }
    
        return $first_image;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Getting image thumb from the post’ is closed to new replies.