Php 使用前端帖子编辑表单更改帖子缩略图
如果用户在前端post编辑屏幕上选择了一个新文件,我将尝试更改post缩略图。这类似于我用来上传数据并在前端添加新帖子表单上设置帖子缩略图的代码:Php 使用前端帖子编辑表单更改帖子缩略图,php,wordpress,file-upload,Php,Wordpress,File Upload,如果用户在前端post编辑屏幕上选择了一个新文件,我将尝试更改post缩略图。这类似于我用来上传数据并在前端添加新帖子表单上设置帖子缩略图的代码: <?php $query = new WP_Query( array( 'post_type' => 'properties', 'posts_per_page' => '-1' ) ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $qu
<?php
$query = new WP_Query( array( 'post_type' => 'properties', 'posts_per_page' => '-1' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
if(isset($_GET['post'])) {
if($_GET['post'] == $post->ID)
{
$current_post = $post->ID;
$content = get_the_content();
$price = get_post_meta($post->ID, 'shru_price', true);
$address = get_post_meta($post->ID, 'shru_address', true);
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-image' );
}
}
endwhile; endif;
wp_reset_query();
global $current_post;
$postContentError = '';
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
if ( trim( $_POST['postContent'] ) === '' ) {
$postContentError = 'Please enter a description of this property.';
$hasError = true;
}
$post_information = array(
'ID' => $current_post,
'post_content' => $_POST['postContent'],
'post_type' => 'properties',
'post_status' => 'publish'
);
$post_id = wp_update_post($post_information);
function upload_user_file( $file = array() ) {
global $post_id;
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$file_return = wp_handle_upload( $file, array('test_form' => false ) );
if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment( $attachment, $file_return['url'], $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if( 0 < intval( $attachment_id ) ) {
return $attachment_id;
}
}
return false;
}
if( ! empty( $_FILES ) ) {
foreach( $_FILES as $file ) {
if( is_array( $file ) ) {
$attachment_id = upload_user_file( $file );
}
}
}
$propertyfor = $_POST['propertyfor'];
$propertytype = $_POST['propertytype'];
$bedrooms = $_POST['bedrooms'];
if($post_id) {
// Update Custom Meta
update_post_meta($post_id, 'shru_price', esc_attr(strip_tags($_POST['shru_price'])));
update_post_meta($post_id, 'shru_address', esc_attr(strip_tags($_POST['shru_address'])));
update_post_meta($post_id, '_thumbnail_id', $attachment_id );
wp_set_object_terms( $post_id, $propertyfor, 'propertyfor' );
wp_set_object_terms( $post_id, $propertytype, 'propertytype' );
wp_set_object_terms( $post_id, $bedrooms, 'bedrooms' );
// Redirect
wp_redirect(home_url('/listings'));
exit;
}
}
?>
唯一的区别是,在上面的代码中,我尝试使用:
update_post_meta($post_id,''u thumbnail_id',$attachment_id)代码>
而不是:
set_post_缩略图($post_id,$attachment_id)代码>
由于某些原因,在后期编辑屏幕上,图像文件甚至不会上载。当我使用update post meta时,它会删除旧的缩略图,所以我猜它在那里做它的工作,但是由于文件没有上传,它不能用新的替换它。令人困惑的是,为什么文件上载使用添加新帖子屏幕上的upload\u user\u file
功能,而不是编辑帖子屏幕
有什么想法吗?这个问题似乎离题了,因为它是在WordPress上提出和回答的--