• Resolved ndreaas

    (@ndreaas)


    Hi guys, I’m now trying to make my own layout, but now I have a little problem.

    I wanted to show the first image in a single post before the post title. This was possible putting this in functions.php

    function catch_that_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)){ //Defines a default image
        $first_img = "/images/default.jpg";
      }
      return $first_img;
    }

    and this in the loop that writes the post in content-single.php

    <img src="<?php echo catch_that_image() ?>" />

    This duplicates the image and presents it before the post title, but then also later in the post where it’s posted.

    I was wondering if there’s a way to remove the image from the text in the post, and only display it above the post title?

    Or is there an easier way to do this?

    (I’m working from localhost on my mac, so I can’t share a link for you to see, unfortunately)

    [please do not bump – if it is that urgent, hire somebody – http://jobs.wordpress.net/ ]

The topic ‘Prevent duplicate using catch_that_image()’ is closed to new replies.