wordpressresizethumbnails

Wordpress featured image set width automatic proportional height


I am resizing my featured images in the functions.php file. I am attempting with ex. "news" to have a set 500 width and auto height - but the outcomes are really weird cropped versions of the image outcome

    if ( function_exists( 'add_theme_support' ) ) {
        add_theme_support( 'post-thumbnails' );
        add_image_size( 'presentation', 9999, 1000, true );
        add_image_size( 'news', 500, 9999, true );
    }

To call the featured image I use this code

<?php 
if ( has_post_thumbnail() ) { 
  the_post_thumbnail('news');
} 
?>

I am trying to have a max width of "500" and the height to be automatically proportional to that width without creating a cropped image.

I tried putting "500" on width and "auto" height but that just creates a "critical error".


Solution

  • What you are seeing is a cropped image because you set the crop argument to true. Using the following code I got an image of 500x667 pixels that looks perfectly fine. Here is the link to the image that was created: http://klaasmaakt.nl/wp-content/uploads/2023/09/Anouk-Pines-2-500x667.jpg

    And this is the code that produces it:

    function add_theme_sizes(){
        if ( function_exists( 'add_theme_support' ) ) {
            add_theme_support( 'post-thumbnails' );
            add_image_size( 'presentation', 9999, 1000, true );
            add_image_size( 'news', 500, 0, false );
        }
    }
    add_action("after_setup_theme", "add_theme_sizes");