簡體   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