• Resolved Anonymous User 5746546

    (@anonymized-5746546)


    Hello, I am trying to get a page to “dynamically” crop a thumbnail. Not sure if I’m explaining it correctly. Maybe some code will help…

    functions.php

    
    add_image_size( 'siteorigin-unwind-1140x200-crop', 1140, 200, true );
    add_image_size( 'siteorigin-unwind-1140x200-crop-top', 1140, 200, array( 'center', 'top' ) );
    add_image_size( 'siteorigin-unwind-1140x200-crop-bottom', 1140, 200, array( 'center', 'bottom' ) );
    

    content-single.php

    
    <?php
    $thumb_type = get_post_meta($post->ID, 'banner_crop', true );
    				
    if ( !empty($thumb_type) ) {
     if ( $thumb_type == "top" ) {
       the_post_thumbnail( 'siteorigin-unwind-1140x200-crop-top', array( 'class' => 'aligncenter' ) );
     } elseif ( $thumb_type == "bottom" ) {
       the_post_thumbnail( 'siteorigin-unwind-1140x200-crop-bottom', array( 'class' => 'aligncenter' ) );
    } else {
       the_post_thumbnail( 'siteorigin-unwind-1140x200-crop', array( 'class' => 'aligncenter' ) );
    }
    ?>
    

    So as you can see, I’ve defined three different custom image sizes. In my single page, I am reading meta data from custom fields. If the value returned is “top”, then use the crop that aligns center and top. If it’s “bottom”, then use the crop that aligns center and bottom. It works on the first try. But when I change the metadata value and refresh the page, nothing happens. It’s stuck with the first result. I tried deleting the image and re-uploading it. But, that did nothing. What am I doing wrong?? ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • vonkoga

    (@vonkoga)

    You’re missing braces at the end to close parent if

    Thread Starter Anonymous User 5746546

    (@anonymized-5746546)

    Hi, I fixed the bracket but the effect is still the same ??

    Thread Starter Anonymous User 5746546

    (@anonymized-5746546)

    Is the problem because I need to regenerate all the images?

    Thread Starter Anonymous User 5746546

    (@anonymized-5746546)

    lol I figured it out. I think it’s a bug. The code in single.php works. But, the code in functions.php doesn’t work because the sizes are all the same like so (I’m repeating the current faulty code):

    
    add_image_size( 'siteorigin-unwind-1140x200-crop', 1140, 200, true );
    add_image_size( 'siteorigin-unwind-1140x200-crop-top', 1140, 200, array( 'center', 'top' ) );
    add_image_size( 'siteorigin-unwind-1140x200-crop-bottom', 1140, 200, array( 'center', 'bottom' ) );
    

    What does fix it and gets everything to work dynamically is the following:

    
    add_image_size( 'siteorigin-unwind-1140x200-crop', 1140, 200, true );
    add_image_size( 'siteorigin-unwind-1140x200-crop-top', 1140, 201, array( 'center', 'top' ) );
    add_image_size( 'siteorigin-unwind-1140x200-crop-bottom', 1140, 202, array( 'center', 'bottom' ) );
    

    Notice how I changed the “200” (from the original code) to 200, 201, and 202 for each of the image sizes. Now everything works. You can add the metadata in the post and pull the image sizes dynamically and you can also change the metadata and it will pull the correct image size.

    Soooo I think there’s a bug.

    Thread Starter Anonymous User 5746546

    (@anonymized-5746546)

    There is currently a ticket of the exact same issue I experienced for an older version: https://core.trac.www.ads-software.com/ticket/40370#comment:5

    gschoppe

    (@gschoppe)

    unrelated to the bug you’re seeing, but if you want to get dynamic cropping working with a little more flexibility, you might want to take a look at https://www.wpsmartcrop.com

    Might make life easier for you.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘re-cropping the_post_thumbnail’ is closed to new replies.