繁体   English   中英

自定义 WordPress 图片大小不显示

[英]Custom WordPress Image size not displaying

我正在尝试在 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'),
));
}

当我按预期将图像上传到媒体库时,我可以看到生成了这个额外的大小,但是当我尝试使用高级自定义字段在 PHP 中实现它时,它不起作用。

自定义图像大小为 1000 像素 x 1000 像素。

为了解决问题并缩小问题范围,我将“中”的默认 WP 大小修改为 1000px x 1000px。

在下面的代码中,使用此默认“中”尺寸的顶部图像使用正确的图像在网站上完美呈现,但第二个没有,它只显示完整尺寸的图像。

我无法弄清楚我的自定义图像代码有什么问题,因为它看起来应该非常简单。

<!-- 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>  

我最终通过将其更改为

// 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' ),

    ) );

虽然我仍然不确定为什么第一个不起作用

暂无
暂无

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

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