[Resolved] No feature image uploaded

Home Forums Support [Resolved] No feature image uploaded

Home Forums Support No feature image uploaded

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #465742
    Edin

    I have lots of older posts where I haven’t defined any “featured image”. As a result, I don’t have images in the homepage articles view, on related posts, etc. with these articles.

    Is it somehow possible to “pull” the first image of the post and define it as a “featured image”?

    Thank you in advance!

    #465980
    Leo
    Staff
    Customer Support

    Hi there,

    That would require a bit of custom coding I think.

    Might be worth asking a forum like this: https://stackoverflow.com/

    #475391
    Edin

    Quick question to understand this better and to find a solution:

    The images that are taken from the “featured image” and posted on the homepage articles view and on related posts, are called “WordPress Post Thumbnails”? Right?

    #475711
    Leo
    Staff
    Customer Support

    Yeah I believe so.

    #478568
    Edin

    Okay, then I believe that I’ve found a solution, I just don’t know how to apply it.

    Can you please give me a tip and tell me if this is a valid method?

    I’m supposed to put this into my functions.php

    //function to call first uploaded image in functions file
    function main_image() {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment
    &post_mime_type=image&order=desc');
      if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', true);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $main=wp_get_attachment_url($num);
            $template=get_template_directory();
            $the_title=get_the_title();
        print "<img src='$main' alt='$the_title' class='frame' />";
      endif;
    }

    And here’s what I don’t know HOW to do:

    The code above simply outputs the first image added to an article. Now we need to display this output in your theme. To do that, you will need to edit the theme files where post_thumbnail(); function is used. Replace it with the following code.

    <?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
      echo get_the_post_thumbnail($post->ID);
    } else {
       echo main_image();
    } ?>

    Where do I put this code? And would you say that this works? (Don’t want to break anything, or slow the site down…)

    Thank you!

    #479010
    Tom
    Lead Developer
    Lead Developer
    #480950
    Edin

    Thanks Tom, I tried it and it works. Problem is, it doesn’t take the FIRST image and turns it into “feature” but another, which makes it look weird sometimes.

    Any quick fix to the code to change this?

    add_filter('get_post_metadata', function($value, $object_id, $meta_key, $single) {
    	if ($meta_key !== '_thumbnail_id' || $value) {
    		return $value;
    	}
    
    	preg_match('~<img[^>]+wp-image-(\\d+)~', get_post_field('post_content', $object_id), $matches);
    	if ($matches) {
    		return $matches[1];
    	}
    	return $value;
    }, 10, 4);

    Thanks again!

    #480951
    Edin

    And on posts after the 2nd half of Page 2 and onwards, it doesn’t work at all!

    That’s weird…

    #481497
    Tom
    Lead Developer
    Lead Developer

    Strange, let me look into this a bit more and get back to you.

    Might be worth asking over on https://wordpress.stackexchange.com/ as well.

    #482087
    Tom
    Lead Developer
    Lead Developer

    I can’t really find any alternative methods – did you have any luck posting on stackexchange?

    #485079
    Edin

    Tom, I think I’m going to do it the old-fashioned way and go through every article and do it manually. It’s 200+ articles, so this can take a while…

    #485086
    Tom
    Lead Developer
    Lead Developer
    #485118
    Edin

    Thank you Tom, the feature I need (select 1st images as featured) is only available in the premium version.

    I’m going to go for manual, shouldn’t take more than an hour, and then it’s done.

    Do you know any way to update the featured image, WITHOUT changing the modified_time we set up here: https://generatepress.com/forums/topic/structured-data-errors-showing-in-google-search-console-webmaster-tools/

    #485602
    Tom
    Lead Developer
    Lead Developer

    I don’t believe that’s possible unfortunately.

    #486262
    Edin

    Tom, I’ve just learned that this plugin you’ve mentioned “quick featured images” offers a way to quickly change the images in the post-list view, WITHOUT changing the date.

    So awesome!

    Thanks again for the help!

    Edin

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.