### [wordpress前端图片怎么上传](https://www.prosaas.cn/article/8246) **Published:** 2021-06-27T08:24:48 **Author:** 5201314 **Excerpt:** WordPress教程栏目给大家介绍wordpress前端图片怎么上传。wordpress前端图片上传最近研究… **WordPress教程**栏目给大家介绍wordpress前端图片怎么上传。 **wordpress前端图片上传** 最近研究个项目需要在wordpress前端上传用户头像,在网上查了些资料!解决了这个问题! 1:首先就是在需要的地方添加文件上传框了
2: 对图片进行处理 $post=get\_post(13);//测试用 if ( $\_FILES ) { $files = $\_FILES\['files'\]; $count= count($files\['name'\]); foreach ($files\['name'\] as $key => $value) { if ($files\['name'\]\[$key\]) { $file = array( 'name' => $files\['name'\]\[$key\], 'type' => $files\['type'\]\[$key\], 'tmp\_name' => $files\['tmp\_name'\]\[$key\], 'error' => $files\['error'\]\[$key\], 'size' => $files\['size'\]\[$key\] ); $\_FILES = array("files" => $file); foreach ($\_FILES as $file => $array) { $newupload = insert\_attachment($file,$post->ID);//此方法将文章附加到ID为13的文章中。如果不想插入到文章可以为空"" } } } } 3:在functions.php文件添加功能函数 insert\_attachment该函数的第二个参数如果为空将不附加到文章中图片。 function insert\_attachment($file\_handler,$post\_id,$setthumb='false') { global $wpdb; // check to make sure its a successful upload if ($\_FILES\[$file\_handler\]\['error'\] !== UPLOAD\_ERR\_OK) \_\_return\_false(); require\_once(ABSPATH . "wp-admin" . '/includes/image.php'); require\_once(ABSPATH . "wp-admin" . '/includes/file.php'); require\_once(ABSPATH . "wp-admin" . '/includes/media.php'); $attach\_id = media\_handle\_upload( $file\_handler, $post\_id ); $image\_url = wp\_get\_attachment\_image\_src( $attach\_id,'full' ); if ($setthumb){ $wpdb->insert( $wpdb->prefix . 'postmeta', array( 'post\_id' => $post\_id, 'meta\_key' => 'wpcf-vi-img', 'meta\_value' => $image\_url\[0\] )); } return $attach\_id; } 4:引用方法 $image\_url = wp\_get\_attachment\_image\_src( $attach\_id,'full' );//由于页面刷新的问题直接在页面使用这个方法是不生效的!需要在函数中构造此方法的功能。 //循环文章中的特征图片的方法,如果将图片附加到文章中使用这个方法可以批量输出! $imagess=get\_post\_meta(13,'wpcf-vi-img',false); foreach($imagess as $images){ echo $images; } 以上就是wordpress前端图片怎么上传的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** WordPress ---