- This topic has 8 replies, 2 voices, and was last updated 6 years, 10 months ago by Tom.
-
AuthorPosts
-
October 27, 2017 at 7:08 am #411289Ramesh Srinivasan
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.
October 27, 2017 at 9:27 am #411448TomLead DeveloperLead DeveloperHi 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 🙂
October 27, 2017 at 9:46 am #411456Ramesh SrinivasanHi 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.
October 27, 2017 at 11:27 am #411493Ramesh SrinivasanTom,
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(); } ?>
October 27, 2017 at 6:57 pm #411677TomLead DeveloperLead DeveloperUnfortunately 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.October 27, 2017 at 11:13 pm #411735Ramesh SrinivasanThanks 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.
October 28, 2017 at 8:03 pm #412141TomLead DeveloperLead DeveloperI 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.
October 28, 2017 at 11:30 pm #412193Ramesh SrinivasanThanks Tom. Will work on that.
October 29, 2017 at 9:25 am #412456TomLead DeveloperLead DeveloperNo problem 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.