删除图片大小在子主题funcions.php中添加图像大小不起作用 [英] remove image size & add image size in child theme funcions.php not working

查看:65
本文介绍了删除图片大小在子主题funcions.php中添加图像大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以提供以下帮助吗?

can anybody help with the following?

我正在尝试更改子主题中主题生成的自定义缩略图大小之一,在子主题中的functions.php中添加了以下内容:

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 );

paren主题的原始功能是:

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 );

但是我的 sela-grid-thumbnail图像仍以父级的原始尺寸显示主题的functions.php-有人可以告诉我为什么吗?我想念什么吗?谢谢

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:在家长之后加载新的尺寸

您需要确保在父声明后 后重新定义图像大小。那是什么意思

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

调用 add_image_size()时,所做的只是获取参数并将其存储到名为<$的全局变量中c $ c> $ _ wp_additional_image_sizes ,由您提供的名称作为关键字。如果您有密钥,则可以覆盖参数(请稍后记住)。

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).

让我们逐步完成加载过程。 WordPress会加载插件,然后是子主题,然后是父主题。考虑一下。您要更改父级创建的默认图像尺寸。对?这意味着:父主题需要先运行,然后再尝试进行更改。

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.

第2步:您无需删除它

您不需要删除图像大小。相反,只要运行 add_image_size 即可。

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

为什么?请记住,我在上面说过,当运行 add_image_size 时,WordPress会覆盖您称为图像大小的名称所存储的内容。当您在子主题中调用它时,它将覆盖存储在全局 $ _ wp_additional_image_sizes 中的父主题。

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.

为使您形象化,这是WordPress核心中的代码:

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.

何时要使用 remove_image_size()?

在不重新定义大小时执行此操作。您只想将其完全删除。

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

第3步:需要重新生成现有图像

添加新图像尺寸时,媒体库和 wp-content / uploads 中的现有图像不会自动生成新尺寸。

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.

这是什么意思?

何时声明了新的图像大小,所有新图像均以声明的每个大小存储在 uploads 文件夹中。打开文件夹,看看。您将看到原始图像文件,然后看到带有附加每个大小的后缀的其他文件。 WordPress正在调整大小,然后将每个图像大小存储到 Uploads 文件夹中。

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.

如何重新生成现有图像

好的,如何重新生成所有现有图像以得到新的尺寸?我使用的是Viper的重新生成缩略图插件。它将遍历所有图像,然后创建新的大小。

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 do you verify that your new size has been registered?

注册存储在全局变量 $ _ wp_additional_image_sizes 中,并以您为其指定的名称为键,即'sela-grid-thumbnail'。要查看其是否已注册,您可以执行以下操作:

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');
}

如果已注册,则将尺寸回显到浏览器。您可以检查它们以确保它们是您想要的尺寸,即242 x 242。

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.

它是否回显了242 x 242尺寸?

Did it echo out 242 x 242 size?


  • 如果是,则将注册您的图像大小并且正确执行了操作。

  • 如果否,那么您需要返回并检查执行 add_image_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().

您在上方和图像尺寸已正确注册。但是,如果在浏览器中看到的图像大小未达到242x242,该怎么办?

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_post_thumbnail()。第一个参数是大小,它是 sela-grid-thumbnail 。检查您要调用的代码以渲染帖子缩略图。确保已指定大小。

  2. CSS会覆盖该大小。在Chrome或Firefox中打开开发人员工具,然后检查图像的URL是否附加了 -242x242.jpg 后缀。

  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.



WordPress如何渲染图像



当渲染图像时,将其设置为'sela-grid-thumbnail'作为大小,WordPress从 uploads 文件夹中抓取适当的文件。假设图片是 my-cool-background.jpg 。 WordPress将抓取 my-cool-background-242x242.jpg

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.

您可以通过打开Chrome或Firefox中的开发者工具。检查浏览器中是否包含正确的图像文件,这意味着该文件具有后缀 -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.

这篇关于删除图片大小在子主题funcions.php中添加图像大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆