Change display size for post image
-
I’m looking for a way to filter/change the current featured image size from a custom function.
I’ve used this piece of code to force the use of featured images from a looong way back:
function wpforce_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } else { set_post_thumbnail($post->ID, '414'); } } } //end function add_action('the_post', 'wpforce_featured'); add_action('save_post', 'wpforce_featured'); add_action('draft_to_publish', 'wpforce_featured'); add_action('new_to_publish', 'wpforce_featured'); add_action('pending_to_publish', 'wpforce_featured'); add_action('future_to_publish', 'wpforce_featured');
Works great but throws out the images in full size. I’m looking for a way to change that through filters or a function. The best solution (kind of) I’ve found is this:
function change_image_size( $html ) { $html = preg_replace( '/(width|height)="\d*"\s/', "width='200' height='50'", $html ); return $html; } add_filter( 'post_thumbnail_html', 'change_image_size', 10 );
This works but it changes the size of the images everywhere – sidebar, footer etc. I just need a way to change the first featured image in every post.
Any help will be appreciated.
- The topic ‘Change display size for post image’ is closed to new replies.