-1

I would like the theme to fetch the first image URL in the post area and display it as the featured image. This came from an old Affiliates WordPress theme functions.php file. This company has closed down.

This is the code I am dealing with:

function pixr_get_thumb( $post )
{
    $wprobot = get_post_meta( $post->ID, "pixr_wpr_thumb", true );
    $wpzon = get_post_meta( $post->ID, "amzn_MediumImageURL", true );
    $goliath = get_post_meta( $post->ID, "amazon-image-url", true );
    $reviewazon = get_post_meta( $post->ID, "ReviewAZON_MediumImage", true );
    $amaniche = get_post_meta( $post->ID, "product_image", true );
    if ( has_post_thumbnail( ) )
    {
        $image_id = get_post_thumbnail_id( );
        $image_url = wp_get_attachment_image_src( $image_id, "blog-thumb", true );
        $image_url = $image_url[0];
    }
    $manual = $image_url;
    $pixrdefault = get_post_meta( $post->ID, "pixr_thumb", true );
    $nothumb = get_bloginfo( "template_directory" )."/assets/img/nothumb.png";
    if ( $wprobot )
    {
        $thumb = $wprobot;
    }
    else if ( $wpzon )
    {
        $thumb = $wpzon;
    }
    else if ( $goliath )
    {
        $thumb = $goliath;
    }
    else if ( $reviewazon )
    {
        $thumb = $reviewazon;
    }
    else if ( $amaniche )
    {
        $thumb = $amaniche;
    }
    else if ( $manual )
    {
        $thumb = $manual;
    }
    else if ( $pixrdefault )
    {
        $thumb = $pixrdefault;
    }
    else
    {
        $thumb = $nothumb;
    }
    return $thumb;
}

function pixr_azon_thumb( )
{
    global $post;
    $thumb = pixr_get_thumb( $post );
    echo $thumb;
}

function pixr_get_image( $post )
{
    $wprobot = get_post_meta( $post->ID, "pixr_wpr_image", true );
    $wpzon = get_post_meta( $post->ID, "amzn_LargeImageURL", true );
    $goliath = get_post_meta( $post->ID, "amazon-image-url-large", true );
    $reviewazon = get_post_meta( $post->ID, "ReviewAZON_LargeImage", true );
    $amaniche = get_post_meta( $post->ID, "image_large_1", true );
    if ( has_post_thumbnail( ) )
    {
        $image_id = get_post_thumbnail_id( );
        $image_url = wp_get_attachment_image_src( $image_id, "product-large", true );
        $image_url = $image_url[0];
    }
    $manual = $image_url;
    $pixrdefault = get_post_meta( $post->ID, "pixr_image", true );
    $nothumb = get_bloginfo( "template_directory" )."/assets/img/nothumb.png";
    if ( $wprobot )
    {
        $image = $wprobot;
    }
    else if ( $wpzon )
    {
        $image = $wpzon;
    }
    else if ( $goliath )
    {
        $image = $goliath;
    }
    else if ( $reviewazon )
    {
        $image = $reviewazon;
    }
    else if ( $amaniche )
    {
        $image = $amaniche;
    }
    else if ( $manual )
    {
        $image = $manual;
    }
    else if ( $pixrdefault )
    {
        $image = $pixrdefault;
    }
    else
    {
        $image = $nothumb;
    }
    return $image;
}
0

First, paste this function on your functions.php file.

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];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

Once done, you can simply call the function within the loop to display the first image from the post:

echo catch_that_image();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.