Archived topic
Featured Image & Post Thumbnails -- Two Questions
8 replies · Started by Ramesh Srinivasan on October 27, 2017
Hi,
I have two queries about Post thumbnails.
1. In the customizer, will setting the thumbnail width/height and clicking "Apply.." generate new thumbnail images? Can the system use the original image (but resize it to specified dimension dynamically without saving thumbnail files) as post thumbnail on the home page?
2. For posts without any feature image set, can GP pick the first image in the post and use it for Post thumbnails? Pls note that I don't want to set any featured image for those posts, yet want the post thumbnails.
Thanks Tom/GP Team.
Hi there,
1. Yes, that option generates new images if no images that size exist. If you just want to resize the image using the original image, you can use some CSS:
.post-image img {
max-width: 200px;
}
2. GP can't, but I do believe there are plugins that will do this for you.
Let me know if you need more info :)
Hi Tom,
If I understand it correctly, I should leave the image width to 0 and use the CSS instead. Let me try that out.
[Update: Works perfectly!]
2. GP can’t, but I do believe there are plugins that will do this for you.
Ok. Will check if there are any Plugins out there.
Thanks Tom.
Tom,
I found a PHP function that does it. (Src: WPBeginner)
Can I add the function using "Code Snippets" Plugin so that I don't have to edit GP's PHP files.
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
echo get_the_post_thumbnail($post->ID);
} else {
echo main_image();
} ?>
Unfortunately not - you would need to use some sort of filter in order to do it without altering theme files. I think it's the post_thumbnail_html filter you would be altering.
Thanks Tom.
Solved it partially..
add_filter('get_post_metadata', function($value, $object_id, $meta_key, $single) {
if (is_home() || is_front_page() || is_archive()){
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);
That worked.. but it ignores the featured image (if already set) and uses only the 1st image for Post Thumbs. Have to add an If condition somewhere in the code.. Might figure out soon.
Thanks again for the help Tom.
I would actually go in this direction: https://wordpress.stackexchange.com/a/55494
Instead of defining a default image, you would need to work your magic to grab the first image in the content.
Thanks Tom. Will work on that.
No problem :)