treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress Featured Image Dynamic resizing

  • Hi guys,

    I'm currently working on a wordpress theme and stumbled on this problem. Do you guys have an idea on how to dynamically resize the image that I've uploaded using the Wordpress Featured Image function?

    Hope you guys could help? Thanks in advance :)

    Best,

    Val
  • Do you mean when you resize your browser or when the image is uploaded?
  • I meant for when the image is uploaded :)
  • Much easier to explain then!

    You'll want to add this to your functions.php file (you can find that in your theme's folder):
    // add post-thumbnail functionality
    if (function_exists('add_theme_support')) { add_theme_support('post-thumbnails'); }
    set_post_thumbnail_size( 100, 100, true ); // Normal post thumbnails
    add_image_size( 'custom-1', 125, 125, true ); // Custom size that will crop the image to fit the proportion
    add_image_size( 'custom-2', 255, 236, false ); // Default is false, "soft proportional crop"

    If you don't yet have a functions.php file in your theme's folder, you can simply create one and add the code above. If you already have one and it's using featured images already, then chances are you'll just have to add the add_image_size stuff.

    To call a featured image with a specific size in your templates, you can use this:
    <?php the_post_thumbnail('custom-1); ?>
  • Wow, cool! This is really a big help. Thank you so much TheDoc. Really appreciate it. :)

    More power to Chris and CSS-Tricks site! :D

    Val
  • So what about when the browser is resized? I'm trying to figure out away to serve up the smaller size image for mobile devices and the larger size on desktop sizes. Ideas? Oh complicate it by stating that I also want to be able to use a caching plugin like w3Total Cache or some other cache.

  • Sir You Have Missed --> ' <-- after the "custom-1" in <?php the_post_thumbnail('custom-1); ?> so i hope you will fix it soon and i dont want other to suffer syntax problem as i have faced.