我正在尝试创建一个自定义字段,当编辑器加载时,如果存在,则将在帖子中填充具有页面/帖子特色图像的URL。 load_field函数的ACF文档全部使用selects而不是普通的文本字段。
我试图做类似的事情:
function my_acf_load_field( $field ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
$field['feat_image_file_name'] = $file;
return $field;
}
// name
add_filter('acf/load_field/name=feat_image_file_name', 'my_acf_load_field');
该页面未填充该值(即使该帖子确实分配了壮举图像)。为我指明正确方向的任何帮助都会很棒。谢谢。
答案 0 :(得分:0)
找到了解决方案,这要归功于另一位开发人员的帮助。解决方案如下:
/* ---------------------------------------------------------------------------
* AUTO-POPULATE ACF Field with FEATURED IMAGE URL PATH
* --------------------------------------------------------------------------- */
function getThumbURL()
{
$myfile = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ));
$finalresult = "";
if(count($myfile) > 0 && is_string($myfile[0]))
{
// get the domain site url and strip it out of the result for relative path only
$domain = get_site_url();
$finalresult = str_replace($domain, '', $myfile[0]);
}
else
{
// Placeholder image if no feat image assigned
$finalresult = '/dir/to/placeholder/img.jpg';
}
return $finalresult;
}
function my_acf_update_value( $value, $post_id, $field ) {
$value = getThumbURL();
return $value;
}
add_filter('acf/update_value/name=feat_image_file_name', 'my_acf_update_value', 10, 3);