0

我正在尝试从前端 ACF 表单上的 ACF 图片库字段中获取第一张图片,并将其用作帖子的特色图片。

我正在为画廊使用“图像数组”返回格式,它也位于“组”字段中。

如果有人能对我的问题有所了解,将不胜感激。

这是我在functions.php中的代码:

// Create a featured image from the first image in the gallery.
add_action('acf/save_post', 'gallery_to_thumbnail');
function gallery_to_thumbnail() {

    if ( have_rows( 'listing_media' ) ) :
        while ( have_rows( 'listing_media' ) ) :
            the_row();
            
            $listing_image_gallery = get_sub_field( 'listing_image_gallery' );
            if ( $listing_image_gallery) :
                $gallery  = get_sub_field( 'listing_image_gallery', false, false );
                $image_id = $gallery[0];
                set_post_thumbnail($post_id, $image_id);                
            endif;
            
        endwhile;
    endif;  
}
4

0 回答 0