简体   繁体   English

自定义 WordPress 图片大小不显示

[英]Custom WordPress Image size not displaying

I am trying to add a custom image size in WordPress like so:我正在尝试在 WordPress 中添加自定义图像尺寸,如下所示:

// Add custom image sizes
if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' ); // The important part
    }


if ( function_exists( 'add_image_size' ) ) {
    add_image_size( 'home_portrait size', 1000, 1000, false );
}
/* Display Custom Image Sizes */
add_filter('image_size_names_choose','wpshout_custom_sizes');
function wpshout_custom_sizes($sizes){
    return array_merge($sizes, array(
        'home_portrait' => __('Home Portrait standard'),
));
}

I can see this extra size is generated when I upload an image to the media library as expected but it does not work when I try and implement it in PHP with advanced custom fields.当我按预期将图像上传到媒体库时,我可以看到生成了这个额外的大小,但是当我尝试使用高级自定义字段在 PHP 中实现它时,它不起作用。

The custom image size is 1000px by 1000px.自定义图像大小为 1000 像素 x 1000 像素。

In an attempt to trouble shoot and narrow down the issue, I amended the default WP size of 'medium' to also be 1000px by 1000px.为了解决问题并缩小问题范围,我将“中”的默认 WP 大小修改为 1000px x 1000px。

In the code below, the top image which uses this default 'medium' size renders perfectly on the website using the correct image but the second does not, it just displays the full size image.在下面的代码中,使用此默认“中”尺寸的顶部图像使用正确的图像在网站上完美呈现,但第二个没有,它只显示完整尺寸的图像。

I can't work out what is wrong with my custom image code given that it seemingly should be very straight forward.我无法弄清楚我的自定义图像代码有什么问题,因为它看起来应该非常简单。

<!-- THIS WORKS -->
                                    
                                    <?php 
                                        $top_image = get_field('top_animated_image_1'); 
                                    ?>  
                                    
                                    <div class="upper-image">
                                            <?php $top_animated_image_1 = get_field( 'top_animated_image_1' ); ?>
                                            <?php $size = 'medium'; ?>
                                            <?php if ( $top_animated_image_1 ) : ?>
                                                <?php echo wp_get_attachment_image( $top_animated_image_1, $size ); ?>
                                            <?php endif; ?>
                                    </div>
                                    
                                    <!-- THIS DOES NOT -->
                                    
                                    <?php 
                                        $lower_image = get_field('lower_animated_image_2'); 
                                    ?>  
                                    
                                    
                                    <div class="lower-image">
                                        <?php $lower_animated_image_2 = get_field( 'lower_animated_image_2' ); ?>
                                        <?php $size = 'home_portrait'; ?>
                                        <?php if ( $lower_animated_image_2 ) : ?>
                                            <?php echo wp_get_attachment_image( $lower_animated_image_2, $size ); ?>
                                        <?php endif; ?>
                                    </div>  

I got this working in the end by changing it to我最终通过将其更改为

// Make sure featured images are enabled
add_theme_support( 'post-thumbnails' );

// Add featured image sizes
add_image_size( 'home_portrait', 1000, 1000, false ); // width, height, crop



// Register the three useful image sizes for use in Add Media modal
add_filter( 'image_size_names_choose', 'wpshout_custom_sizes' );
function wpshout_custom_sizes( $sizes ) {
    return array_merge( $sizes, array(
        'home_portrait' => __( 'Home Portrait' ),

    ) );

though I am still not sure why the first did not work虽然我仍然不确定为什么第一个不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM