从客串形式添加图像到post_content

我试图让客人通过帖子类别在我的wordpress页面中发布活动,他们需要为帖子设置帖子缩略图和1-3张图像。我添加了带有set_post_thumbnail()的帖子缩略图,但我无法弄清楚并在wordpress手札中找到一些东西来帮助我,也许你们知道一些东西可以帮助我走上正确的道路?到目前为止,我的代码:


//Upload Image

    require_once( ABSPATH . 'wp-admin/includes/image.php' );

    require_once( ABSPATH . 'wp-admin/includes/file.php' );

    require_once( ABSPATH . 'wp-admin/includes/media.php' );

    $attachment_id = media_handle_upload('file',$post_id);

    $attachment_id2 = media_handle_upload('file2',$post_id);

    $attachment_id3 = media_handle_upload('file3',$post_id);

    $attachment_id4 = media_handle_upload('file4',$post_id);


//Set Image as thumbnail

    set_post_thumbnail($post_id, $attachment_id); 


牧羊人nacy
浏览 115回答 1
1回答

潇潇雨雨

我发现这个问题有点模糊,所以我会用我认为你的意思来回答。首先,您需要表单上的属性。enctype<form ... enctype="multipart/form-data">&nbsp; &nbsp; <input name="file" type="file">&nbsp; &nbsp; <input name="file2" type="file">&nbsp; &nbsp; <input name="file3" type="file">&nbsp; &nbsp; <input name="file4" type="file"></form>现在,在您的流程文件中,您需要从数组中获取每个文件。$_FILES<?php&nbsp; &nbsp; $file = !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null;&nbsp; &nbsp; $file2 = !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null;&nbsp; &nbsp; $file3 = !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null;&nbsp; &nbsp; $file4 = !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null;最后,您需要一个自定义函数来处理每个文件的上传。这是我在我的项目中创建和使用的那一个(针对此问题进行了修改,因此请进行测试!<?phpfunction my_asset_uploader( $file, $parent_id = 0, $allowed = [] ) {&nbsp; &nbsp; require_once ABSPATH . 'wp-admin/includes/file.php';&nbsp; &nbsp; require_once ABSPATH . 'wp-admin/includes/image.php';&nbsp; &nbsp; // Get basic attributes&nbsp; &nbsp; $filename&nbsp; &nbsp;= basename( $file[ 'name' ] );&nbsp; &nbsp; $mime&nbsp; &nbsp; &nbsp; &nbsp;= wp_check_filetype( $filename );&nbsp; &nbsp; // Get the content type from response headers&nbsp; &nbsp; $type&nbsp; &nbsp;= !empty( $mime[ 'type' ] ) ? $mime[ 'type' ] : $file[ 'type' ];&nbsp; &nbsp; $ext&nbsp; &nbsp; = !empty( $mime[ 'ext' ] ) ? $mime[ 'ext' ] : trim( explode( '|' , array_search( $type, get_allowed_mime_types() ) )[ 0 ] );&nbsp; &nbsp; // Basic checks&nbsp; &nbsp; if ( !$type || !$ext || ( !empty( $allowed ) && is_array( $allowed ) && !in_array( $ext, $allowed ) ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Not a valid file&nbsp; &nbsp; &nbsp; &nbsp; return new WP_Error( 'upload', 'Invalid file type. Please try another file.' );&nbsp; &nbsp; }&nbsp; &nbsp; // Move file to wp-content&nbsp; &nbsp; $body&nbsp; &nbsp;= @file_get_contents( $file[ 'tmp_name' ] );&nbsp; &nbsp; $file&nbsp; &nbsp;= wp_upload_bits( $filename, null, $body );&nbsp; &nbsp; // Upload check&nbsp; &nbsp; if ( !empty( $file[ 'error' ] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; return new WP_Error( 'upload', $file[ 'error' ] );&nbsp; &nbsp; }&nbsp; &nbsp; // Write attachment location to the database&nbsp; &nbsp; $attachment_id = wp_insert_attachment( [&nbsp; &nbsp; &nbsp; &nbsp; 'post_title'&nbsp; &nbsp; &nbsp; &nbsp; => $filename,&nbsp; &nbsp; &nbsp; &nbsp; 'post_mime_type'&nbsp; &nbsp; => $file[ 'type' ]&nbsp; &nbsp; ], $file[ 'file' ], $parent_id, true );&nbsp; &nbsp; if ( is_wp_error( $attachment_id ) ) {&nbsp; &nbsp; &nbsp; &nbsp; return $attachment_id;&nbsp; &nbsp; }&nbsp; &nbsp; // Generate meta&nbsp; &nbsp; $attach_data = wp_generate_attachment_metadata( $attachment_id, $file[ 'file' ] );&nbsp; &nbsp; wp_update_attachment_metadata( $attachment_id, $attach_data );&nbsp; &nbsp; return $attachment_id;}用法<?php/**&nbsp;* Extensions we allow the user to upload&nbsp;*/$allowed_extensions = [ 'jpg', 'jpeg', 'png', 'gif' ];/**&nbsp;* Insert post&nbsp;*/$post_id = wp_insert_post( ... );/**&nbsp;* Get all files from the request payload&nbsp;*/$all_images = array_filter( [&nbsp; &nbsp; !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null,&nbsp; &nbsp; !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null,&nbsp; &nbsp; !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null,&nbsp; &nbsp; !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null,] );/**&nbsp;* Uploaded files&nbsp;*/$attachment_ids = [];/**&nbsp;* Check we have any images before looping&nbsp;*/if ( $all_images ) {&nbsp; &nbsp; foreach ( $all_images as $img_file ) {&nbsp; &nbsp; &nbsp; &nbsp; $this_id = my_asset_uploader( $img_file, $post_id, $allowed_extensions );&nbsp; &nbsp; &nbsp; &nbsp; if ( $this_id && !is_wp_error( $this_id ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attachment_ids[] = $this_id;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/**&nbsp;* Check at least one image was successfully uploaded&nbsp;*/if ( !empty( $attachment_ids[ 0 ] ) ) {&nbsp; &nbsp; set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );}这只是一个起点,引导您朝着正确的方向前进。希望这有帮助!编辑将三个图像添加到post_content...如果使用上述过程并且您拥有附件ID数组,则可以将每个附件的图像html附加到帖子内容中,如下所示:<?php.../**&nbsp;* Check at least one image was successfully uploaded&nbsp;*/if ( !empty( $attachment_ids[ 0 ] ) ) {&nbsp; &nbsp; set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );}/**&nbsp;* We have multiple files, lets append them to the post_content&nbsp;*/if ( count( $attachment_ids ) > 1 ) {&nbsp; &nbsp; $appendage = '';&nbsp; &nbsp; foreach ( $attachment_ids as $i => $img_id ) {&nbsp; &nbsp; &nbsp; &nbsp; // Skip the first image because we used that as the thumbnail&nbsp; &nbsp; &nbsp; &nbsp; if ( $i === 0 ) continue;&nbsp; &nbsp; &nbsp; &nbsp; // Get the attachment HTML for a `large` sized image&nbsp; &nbsp; &nbsp; &nbsp; if ( $html = wp_get_attachment_image( $img_id, 'large' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $appendage .= $html;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if ( !empty( $appendage ) ) {&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Get the current post content&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; $current_content = get_post_field( 'post_content', $post_id, 'raw' );&nbsp; &nbsp; &nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* Update the post&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; wp_update_post( [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'ID' => $post_id,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_content' => ( $current_content . $appendage ) // Joining the two strings&nbsp; &nbsp; &nbsp; &nbsp; ] );&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP