- This topic has 15 replies, 3 voices, and was last updated 6 years, 2 months ago by Tom.
-
AuthorPosts
-
January 8, 2018 at 6:12 am #465742Edin
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!
January 8, 2018 at 9:35 am #465980LeoStaffCustomer SupportHi there,
That would require a bit of custom coding I think.
Might be worth asking a forum like this: https://stackoverflow.com/
January 20, 2018 at 12:49 am #475391EdinQuick 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?
January 20, 2018 at 8:40 am #475711LeoStaffCustomer SupportYeah I believe so.
January 23, 2018 at 11:36 pm #478568EdinOkay, 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!
January 24, 2018 at 9:22 am #479010TomLead DeveloperLead DeveloperSomething like this might be better: https://www.wptutor.io/wordpress/snippets/first-image-thumbnail
January 27, 2018 at 1:37 am #480950EdinThanks 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!
January 27, 2018 at 1:40 am #480951EdinAnd on posts after the 2nd half of Page 2 and onwards, it doesn’t work at all!
That’s weird…
January 28, 2018 at 12:54 am #481497TomLead DeveloperLead DeveloperStrange, 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.
January 28, 2018 at 7:42 pm #482087TomLead DeveloperLead DeveloperI can’t really find any alternative methods – did you have any luck posting on stackexchange?
January 31, 2018 at 10:58 pm #485079EdinTom, 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…
January 31, 2018 at 11:15 pm #485086TomLead DeveloperLead DeveloperThis might be worth checking out: https://en-ca.wordpress.org/plugins/quick-featured-images/
February 1, 2018 at 12:39 am #485118EdinThank 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/
February 1, 2018 at 9:38 am #485602TomLead DeveloperLead DeveloperI don’t believe that’s possible unfortunately.
February 2, 2018 at 3:22 am #486262EdinTom, 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
-
AuthorPosts
- You must be logged in to reply to this topic.