Results 1 to 2 of 2

Thread: Fetch/Retrieve 1st image in post and set it as featured image in wordpress

  1. #1
    Moderator A Khan's Avatar
    Join Date
    Nov 2012
    ΑΡ Credit

    Fetch/Retrieve 1st image in post and set it as featured image in wordpress

    If you have a website have images in posts, but you had not set featured images for each post in past. And now you want to use featured image feature on wordpress. Then it will be very annoying for you to edit every post and set feature images for every single post. So here is a theme modification method for you, which help you to get the feature images from old posts.

    Here is the code which scans the post, then pulls in the image set as 'featured'. If that doesn't exist, it returns the first image attached to the post. If there isn't one, it looks for any image in the post; if that's not there, it grabs a YouTube screenshot. Put this in your theme's functions.php file:

    PHP Code:
    // Note that your theme must support post thumbnails for this function to work. 
    // If you are getting an error try adding add_theme_support('post-thumbnails'); to your functions. php file  
    function vp_get_thumb_url($text$size){

    // Check to see which image is set as "Featured Image"
    $featuredimg get_post_thumbnail_id($post->ID);
    // Get source for featured image
    $img_src wp_get_attachment_image_src($featuredimg$size);
    // Set $imageurl to Featured Image

    // If there is no "Featured Image" set, move on and get the first image attached to the post
    if (!$imageurl) {
    // Extract the thumbnail from the first attached imaged
    $allimages =&get_children('post_type=attachment&post_mime_type=image&post_parent=' $post->ID );

            foreach (
    $allimages as $img){
    $img_src wp_get_attachment_image_src($img->ID$size);
    // Set $imageurl to first attached image

    // If there is no image attached to the post, look for anything that looks like an image and get that
    if (!$imageurl) {
    preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i' ,  $text$matches);

    // If there's no image attached or inserted in the post, look for a YouTube video
    if (!$imageurl){
    // look for traditional url from address bar
    $youtubeurl $matches2[0];
    $videokey $matches2[3];
        if (!
    $youtubeurl) {
    // look for 'embed' url
    $youtubeurl $matches2[0];
    $videokey $matches2[2];
        if (
    // Get the thumbnail YouTube automatically generates
            // '0' is the biggest version, use 1 2 or 3 for smaller versions
    $imageurl "{$videokey}/0.jpg";

    // Spit out the image path
    return $imageurl;

    To display this in your theme files, first insert the following:

    PHP Code:
    if (function_exists('vp_get_thumb_url')) {
    // Set the desired image size. Swap out 'thumbnail' for 'medium', 'large', or custom size

    and somewhere after that, to display the actual image (in category or single post file) in this case wrapped with a link to the post:
    PHP Code:
    <?php if ($thumb!='') { ?>
                                            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img src="<?php echo $thumb?>" alt="<?php get_the_title(); ?>" /></a>
                                        <?php ?>
    Search Google on the vp_get_thumb_url function name for other articles on how to implement this. Best of luck!

  2. #2
    Join Date
    May 2016
    ΑΡ Credit
    Thank you for thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts