如果您使用save_post
钩子,每次发布或更新内容时,都会刷新特色图像。注意:确保ACF设置为返回图像ID,这不是其默认值。
add_action( \'save_post\', \'wpse_set_featured_image\' );
function wpse_set_featured_image($post_id) {
// get author id
$author_id = get_the_author_id($post_id);
// get author\'s image
$author_image_id = get_user_meta($author_id, \'your_acf_img_var_name\', true);
// fallback image
if(empty($author_image_id)) {
// set to an existing image ID to use as a fallback
$author_image_id = \'4\';
}
// at last, set the post featured image
set_post_thumbnail($post_id, $author_image_id);
}