Home / posts / Get first image in post
  • How to Get first image in post

    no thumbnail sorry: Get first image in post

    Adding this PHP code to your wordpress theme will return the URL for the first image in your post.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('//i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
      // no image found display default image instead
      if(empty($first_img)){
        $first_img = "/images/default.jpg";
      }
      return $first_img;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    _e,