Archived topic
No feature image uploaded
15 replies · Started by Edin on January 8, 2018
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!
Hi there,
That would require a bit of custom coding I think.
Might be worth asking a forum like this: https://stackoverflow.com/
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?
Yeah I believe so.
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!
Something like this might be better: https://www.wptutor.io/wordpress/snippets/first-image-thumbnail
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!
And on posts after the 2nd half of Page 2 and onwards, it doesn't work at all!
That's weird...
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.
I can't really find any alternative methods - did you have any luck posting on stackexchange?
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...
This might be worth checking out: https://en-ca.wordpress.org/plugins/quick-featured-images/
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/
I don't believe that's possible unfortunately.
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