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 = '';
      $output = preg_match_all('//i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
      // no image found display default image instead
        $first_img = "/images/default.jpg";
      return $first_img;

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