remove image size & add image size in child theme funcions.php not working

拥有回忆 提交于 2020-07-09 11:42:09

问题


can anybody help with the following?

I'm trying to alter the one of the custom thumbnail sizes produced by a theme in the child theme, i've added the following to functions.php in the child theme:

add_theme_support( 'post-thumbnails' );
    remove_image_size( 'sela-grid-thumbnail' );
    add_image_size( 'sela-grid-thumbnail', 242, 242, true );

The original function from the paren theme is:

add_theme_support( 'post-thumbnails' );

// Post thumbnails
set_post_thumbnail_size( 820, 312, true );
// Hero Image on the front page template
add_image_size( 'sela-hero-thumbnail', 1180, 610, true );
// Full width and grid page template
add_image_size( 'sela-page-thumbnail', 1180, 435, true );
// Grid child page thumbnail
add_image_size( 'sela-grid-thumbnail', 360, 242, true );
// Testimonial thumbnail
add_image_size( 'sela-testimonial-thumbnail', 90, 90, true );

But my 'sela-grid-thumbnail' images are still coming out at the original size from the parent theme's functions.php - can anybody tell me why? Am i missing something? Thank you


回答1:


Step 1: Load Your New Size After the Parent

You need to make sure that you are redefining the image size after the parent has declared it. What do I mean by that?

When calling add_image_size(), all it does is take the parameters and store it into a global called $_wp_additional_image_sizes, which is keyed by the name you give it. You can overwrite the parameters if you have the key (remember this for later).

Let's walk through the loading sequence. WordPress loads the plugins, then the child theme, and then the parent theme. Think about that. You are wanting to change the default image size that the parent is creating. Right? That means: the parent theme needs to run first before you try to change it.

Check your child theme and make sure that you are redefining the image size after the parent theme adds it.

Step 2: You don't need to remove it

You don't need to remove the image size. Instead, just run the add_image_size as you have it.

Why? Remember that I said above, when running add_image_size, WordPress overwrites what is stored by the name you call the image size. When you call it in your child theme, it will overwrite what the parent theme stored in the global $_wp_additional_image_sizes.

To help you visualize this, here is the code from WordPress core:

function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
    global $_wp_additional_image_sizes;

    $_wp_additional_image_sizes[ $name ] = array(
        'width'  => absint( $width ),
        'height' => absint( $height ),
        'crop'   => $crop,
    );
}

Do you see how it just overwrites if the name already exists? Else, it adds it as a new size.

When would you want to remove an image size using remove_image_size()?

Do this when you are not redefining the size. You just want to remove it completely.

Step 3: Need to Regenerate Existing Images

When adding a new image size, the existing images in the media library and in wp-content/uploads does not automatically generate the new sizes. All new images added will be stored with the new size.

What do I mean by that?

When a new image size is declared, all new images are stored in the uploads folder with each of the declared sizes. Open up the folder and take a look. You'll see the original image file and then additional files with each of the sizes appended as a suffix. WordPress is taking care of resizing and then storing each of the image sizes into the Uploads folder.

How to Regenerate Existing Images

Okay, how do you regenerate all of the existing images to get your new size? I use Viper's Regenerate Thumbnails plugin. It will run through all of the images and then create the new sizes.

How to Verify New Size

How do you verify that your new size has been registered?

The registration is stored in the global variable $_wp_additional_image_sizes and keyed by the name you gave it, i.e. 'sela-grid-thumbnail'. To see if it's been registered, you can do this:

add_action('loop_start', 'check_if_image_size_is_registered');
/**
 * Check if the image size has been registered when the
 * Loop first starts.
 *
 * @since 1.0.0
 *
 * @return void
 */
function check_if_image_size_is_registered() {
    global $_wp_additional_image_sizes;

    if ( ! isset( $_wp_additional_image_sizes['sela-grid-thumbnail'] ) ){
        die('nope, not registered');
    }

    echo '<pre>';
    var_dump( $_wp_additional_image_sizes['sela-grid-thumbnail'] );
    echo '</pre>';

    die('yes, it is registered');
}

If it has been registered, then the sizes are echoed out to the browser. You can check those to make sure they are the sizes you wanted, i.e. 242 x 242.

Did it echo out 242 x 242 size?

  • If yes, then your image size is registered and you did it correctly.
  • If no, then you need to go back and check the order of when you did add_image_size().

What if it is registered, but the images are not formatted?

You checked above and the image size is properly registered. But what do you do if the image you see is not sized to 242x242 in the browser?

That's a different problem. It could be:

  1. The image size not specified when rendering out to the browser. For example, let's say you're using the_post_thumbnail(). The first parameter is the size, which would be 'sela-grid-thumbnail'. Check the code where you are calling to render out the post thumbnail. Make sure the size is specified.
  2. CSS is overriding the size. Open up Developer Tools in Chrome or Firefox and check that the image's URL has the -242x242.jpg suffix appended on it.

How WordPress renders the image

When you render out the image and set it as 'sela-grid-thumbnail' for the size, WordPress grabs the appropriate file out of the uploads folder. Let's say the image is my-cool-background.jpg. WordPress would grab the my-cool-background-242x242.jpg.

You can check this by opening up the Developer Tools in Chrome or Firefox. Check that the correct image file is in the browser, meaning it has the -242x242.jpg suffix on it.



来源:https://stackoverflow.com/questions/40790407/remove-image-size-add-image-size-in-child-theme-funcions-php-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!