我有一个表单,其中包含上传($_FILES['watch_photo']
)字段。
我环顾四周,开始把这个功能放在一起。
它基本上会获取所有相关信息,以便将来可以重复使用,它会在完成后返回$ pid和文件的URL数组。
问题是ACF没有提供太多信息,如何使用update_field()
http://www.advancedcustomfields.com/resources/functions/update_field/
function my_update_attachment($f,$pid,$t='',$c='') {
wp_update_attachment_metadata( $pid, $f );
if( !empty( $_FILES[$f]['name'] )) { //New upload
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$override['action'] = 'editpost';
$file = wp_handle_upload( $_FILES[$f], $override );
if ( isset( $file['error'] )) {
return new WP_Error( 'upload_error', $file['error'] );
}
$file_type = wp_check_filetype($_FILES[$f]['name'], array(
'jpg|jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
));
if ($file_type['type']) {
$name_parts = pathinfo( $file['file'] );
$name = $file['filename'];
$type = $file['type'];
$title = $t ? $t : $name;
$content = $c;
$attachment = array(
'post_title' => $title,
'post_type' => 'attachment',
'post_content' => $content,
'post_parent' => $pid,
'post_mime_type' => $type,
'guid' => $file['url'],
);
foreach( get_intermediate_image_sizes() as $s ) {
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => true );
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
}
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
foreach( $sizes as $size => $size_data ) {
$resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
$attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
if ( !is_wp_error( $id )) {
$attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
wp_update_attachment_metadata( $attach_id, $attach_meta );
}
return array(
'pid' =>$pid,
'url' =>$file['url']
);
// update_post_meta( $pid, 'a_image', $file['url'] );
}
}
}
然后我使用了以下代码:
my_update_attachment
处理图片代码:
$args= array( 'post_type' => 'watches', 'post_status' => 'publish' );
$watch = wp_insert_post($args);
$att = my_update_attachment('watch_image',$watch);
update_field('field_512e085a9372a',$att['url'],$watch);
我错过了什么?任何帮助将不胜感激。
答案 0 :(得分:19)
我遇到了同样的问题,你几乎把它弄好了,这对我有帮助。
查看了ACF,update_field
接受了附件ID,与pid
或url
您可以替换返回数组:
return array(
'pid' =>$pid,
'url' =>$file['url']
);
使用以下数组:
return array(
'pid' =>$pid,
'url' =>$file['url'],
'file'=>$file,
'attach_id'=>$attach_id
);
然后更改
update_field('field_512e085a9372a',$att['url'],$watch);
以下
update_field('field_512e085a9372a',$att['attach_id'],$watch);
我也在这里发表了博客http://www.jqui.net/wordpress/acf-upload-image-made-easy/
答案 1 :(得分:0)
应该是这条线
if ( !is_wp_error( $attach_id )) {
成为
body {
background-color: gery;
}
.cards {
margin: 0 auto;
max-width: 1000px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(225px, 1fr));
grid-auto-rows: auto;
gap: 20px;
padding-top: 30px;
}
.card {
background-color: #fff;
line-height: 1.5;
font-size: 1.1em;
padding: 15px;
background: #fafafa;
}
.card__image {
width: 100% !important;
height: 150px;
object-fit: cover;
display: block;
}
答案 2 :(得分:-1)
检查此插件:
https://wordpress.org/plugins/acf-frontend-display/