Jump to content

Wordpress get first image from post but exculde image if word found in img src


pengume

Recommended Posts

Hello I am new to php but understand sometimes  ::)

I am trying to exclude some images from this function that I found by running a preg_match but it seems to never work was hoping someone here could help me out. If you need more explanation just ask. Well here is the code.

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  $new_img_tag = "";

  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  
  $first_img = $matches [1] [0];
  $check_img = preg_match('(pheedo|feedburner)',$first_img);

  if ($check_img == false) {
$new_img_tag = "<img src='http://www.searchforhealth.com/wp-content/uploads/2010/06/featured-default.gif' width='67px' class='alignleft' />";
}
else {
  $new_img_tag = "<img src='" .  $first_img . "' width='300px' class='alignleft' />";
  }
  if(empty($first_img)){ //Defines a default image with 0 width
  $new_img_tag = "<img src='http://www.searchforhealth.com/wp-content/uploads/2010/06/featured-default.gif' width='67px' class='alignleft' />";
  }

  else{
$new_img_tag = "<img src='" .  $first_img . "' width='300px' class='alignleft' />";
         
}

  return $new_img_tag;
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.