WordPress.org

Forums

How do I use "add_image_size" function for more thumbnail sizes. (8 posts)

  1. mxmaniac
    Member
    Posted 1 year ago #

    I have searched for hours on how to add more thumbnail sizes to wordpress and have them work correctly. I found this bit of code that can be added to functions.php (in child theme), and can add one extra size, however I want to add more.

    <?php
    if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'new-size', 650, 650 ); //(cropped)
    }
    add_filter('image_size_names_choose', 'my_image_sizes');
    function my_image_sizes($sizes) {
    $addsizes = array(
    "new-size" => __( "New Size")
    );
    $newsizes = array_merge($sizes, $addsizes);
    return $newsizes;
    }
    ?>

    However I've tried everything I could, and re-read the codex over and over, but just can't understand what I'm doing, and get it to work. Can anyone help explain which extra lines of code will be necessary to add?

  2. pyjamaman
    Member
    Posted 1 year ago #

  3. jessicarenee408
    Member
    Posted 1 year ago #

    function custom_thumbs()
    {
        add_image_size( 'thumb-small', 200, 200, true ); // Hard crop to exact dimensions (crops sides or top and bottom)
        add_image_size( 'thumb-medium', 520, 9999 ); // Crop to 520px width, unlimited height
        add_image_size( 'thumb-large', 720, 340 ); // Soft proprtional crop to max 720px width, max 340px height
    }
    add_action( 'after_setup_theme', 'custom_thumbs' );

    These are a few different examples you can have. Alter this to fit your needs, and place it in your functions.php file.

    Then, to implement, you would need to add this to the .php template calling your thumbnail: <?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'thumb-small' ); } ?>

    Replace 'thumb-small' with the name you made for your new thumbnail.

    If you have existing images that you want this to take place on, you will need to regenerate your thumbnail sizes after you have done the above. Just use this plugin to do so: http://wordpress.org/plugins/regenerate-thumbnails/

  4. Rajan V
    Member
    Posted 1 year ago #

    Or

    Simply add top of your theme function.php

    add_image_size( 'thumb-small', 200, 200, true );

  5. mxmaniac
    Member
    Posted 1 year ago #

    Thanks for the responses. I tried them all with varying results, but wasn't quite sussessful. Many of them do seem to get wordpress to generate the thumbnails, as I can see them when I ftp into my upload folder.

    However when I go to try and use them, when clicking the "add media" button, when making a new post. I'm still generally only given the default sizes.

    The code I had posted in the first post at least gave me one extra one. I could add extra lines of
    'add_image_size( 'new-size', 650, 650 );'
    which seems to work for generating more sizes, yet they won't show up. The bottom part of the code would make at least one size show up, but I have been unable to figure out how to modify it to make all sizes that I add show up.

  6. Dave Naylor
    Member
    Posted 1 year ago #

    You need to add your additional image sizes to the $addsizes array in your above function:

    $addsizes = array( "new-size" => __( "New Size" ),
                        "another-size" => __( "Another Size" )
                        );

    It might be better to give them more descriptive titles.

  7. mxmaniac
    Member
    Posted 1 year ago #

    Well, I seem to be doing something wrong still. Right now I have this in the functions.php file of a child theme. Although wordpress does seem to generate the new sizes, as confirmed by ftp'ing into my upload folder. I still cannot select any new sizes from wordpress when trying to add media into a post. Here is my code so far.

    <?php
    if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'Mysize-200', 200, 200 );
    add_image_size( 'Mysize-400', 400, 400 );
    add_image_size( 'Mysize-500', 500, 500 );
    }
    add_filter('image_size_names_choose', 'my_image_sizes');
    function my_image_sizes($sizes) {
    $addsizes = array(
    "Mysize-200" => __( "Mysize-200" ),
    "Mysize-400" => __( "Mysize-400" ),
    "Mysize-500" => __( "Mysize-500" )
    );
    $newsizes = array_merge($sizes, $addsizes);
    return $newsizes;
    }
    ?>

    Do I have some syntax wrong somewhere, or missing something?

  8. mxmaniac
    Member
    Posted 1 year ago #

    Well, I FINALLY seem to have gotten this to work, with a different code variation. It was quite frustrating because through all my searching, I found so many different variations of the code, yet none of them worked for me. Heres what actually seemed to work for adding 4 new sizes, and is easy to decipher to add more or less.

    if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'Mysize-200', 200, 200 );
    add_image_size( 'Mysize-400', 400, 400 );
    add_image_size( 'Mysize-500', 500, 500 );
    add_image_size( 'Mysize-500tall-1000wide', 1000, 500 );
    }
    add_filter('image_size_names_choose', 'my_image_sizes');
    function my_image_sizes( $sizes ) {
        return array_merge( $sizes, array(
            'Mysize-200' => __('Mysize-200'),
            'Mysize-400' => __('Mysize-400'),
            'Mysize-500' => __('Mysize-500'),
            'Mysize-500tall-1000wide' => __('Mysize-500tall-1000wide'),
        ) );
    }

    Placed that into a child theme's functions.php, and it seems to be working so far.

    Tags: (for anyone else searching for this solution)
    How to use wordpress add_image_sizes
    How to add image sizes to wordpress.
    How to add more thumbnail sizes to wordpress.
    wordpress add_image_size not working
    wordpress add_image_size doesn't work

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.