我正在使用此代码提交一个帖子表单并上载一个图像,我想将其作为帖子缩略图。所有内容都提交得很完美,图像甚至可以上传,但我似乎不知道如何将图像附加到帖子并将其自动设置为帖子缩略图。任何帮助都将不胜感激。
<?php
$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;
}
$random = rand(1000000, 9999999);
$post_information = array(
\'post_name\' => sanitize_title( date( \'YmdHis\' ). \'-\' . $random),
\'post_title\' => \'Property \'.date( \'YmdHis\' ). \'-\' . $random,
\'post_content\' => $_POST[\'postContent\'],
\'post_type\' => \'properties\',
\'post_status\' => \'publish\'
);
$propertyfor = $_POST[\'propertyfor\'];
$propertytype = $_POST[\'propertytype\'];
$bedrooms = $_POST[\'bedrooms\'];
$post_id = wp_insert_post($post_information);
function upload_user_file( $file = array() ) {
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 );
}
}
}
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\'])));
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;
}
}
?>
最合适的回答,由SO网友:s_ha_dum 整理而成
你有一个variable scope problem.
$post_id = wp_insert_post($post_information);
function upload_user_file( $file = array() ) {
require_once( ABSPATH . \'wp-admin/includes/admin.php\' );
$file_return = wp_handle_upload( $file, array(\'test_form\' => false ) );
// ...
您已设置
$post_id
在您的
upload_user_file()
功能,但这意味着它在您需要它的功能中不可用
wp_insert_attachment()
.
如果你有debugging enabled 正如你在工作时应该做的那样,你会立即发现这一点。
此外,还包括几个核心文件,例如,require_once( ABSPATH . \'wp-admin/includes/admin.php\' );
-- 这几乎总是表明你Doing it Wrong. 我觉得你应该使用AJAX API 我猜你在做什么,直接加载一个“handler”文件。