ACF-按字段添加/更新特色图像(用于视频)

时间:2016-12-30 作者:Game Unity

我正在尝试通过ACF字段将缩略图添加到我的视频中。因此,我正在寻找一些PHP来添加/更新视频上的特色图像(在附件详细信息屏幕上)。我在ACF论坛上测试了这段代码,但似乎不起作用

// Set the first sock image uploaded as the featured image
function acf_set_featured_image( $value, $post_id, $field  ){

if($value != \'\'){
  delete_post_thumbnail( $post_id);
  //Add the value which is the image ID to the _thumbnail_id meta data for the current post
  add_post_meta($post_id, \'_thumbnail_id\', $value);
}

return $value;
}

// acf/update_value/name={$field_name} - filter for a specific field based on it\'s name
add_filter(\'acf/update_value/name=video_thumbnail\', \'acf_set_featured_image\', 10, 3);

//field name: video_thumbnail
Update:我真是个白痴…-我编辑了错误的函数。php,因此上面的代码很好:|

注意:我添加此行是为了编辑现有图像。

delete_post_thumbnail( $post_id);

1 个回复
最合适的回答,由SO网友:prosti 整理而成

更新密钥时会出现此代码name.

File: wp-content/plugins/advanced-custom-fields/core/fields/_functions.php
193:        foreach( array(\'key\', \'name\', \'type\') as $key )
194:        {
195:            // run filters
196:            $value = apply_filters(\'acf/update_value/\' . $key . \'=\' . $field[ $key ], $value, $post_id, $field); // new filter
197:        }
尝试:打印$value、$post\\u id、$字段以了解您拥有的内容。。。可能你有一些错误的标签。。。

function acf_set_featured_image( $value, $post_id, $field  ){
// print $value, $post_id, $field to understand what you have...
print_r(...);

wp_die();

if($value != \'\'){
    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    add_post_meta($post_id, \'_thumbnail_id\', $value);
}

return $value;
}

// acf/update_value/name={$field_name} - filter for a specific field based on it\'s name
add_filter(\'acf/update_value/name=video_thumbnail\', \'acf_set_featured_image\', 10, 3);

相关推荐

fetch images and videos

是否可以获取特定网站的视频和图像并将其发布?我的意思是我正在考虑写一个函数,在这里我只写网站名称,然后在网站url的帮助下,所有或最新的图片和视频都会发布到我的帖子上。这可能吗?