Image processing 前端帖子表单要么设置缩略图,要么给我ID,我需要两者
我有一个可湿性粉剂前端后的形式,我已经为我的网站,其想法是使之成为艺术家可以张贴自己的混音提交 到目前为止,我已经设法得到的形式,要么使用作为特色图像上传的图像或给我的图像ID,这样我就可以把它在文章的内容,问题是我需要两者!我需要的图像自动设置为缩略图的职位,我需要的ID,以便我可以设置的形式,以自动把图像在我想要的地方张贴 到目前为止,我发现问题在于这段代码:Image processing 前端帖子表单要么设置缩略图,要么给我ID,我需要两者,image-processing,wordpress,frontend,attachment,Image Processing,Wordpress,Frontend,Attachment,我有一个可湿性粉剂前端后的形式,我已经为我的网站,其想法是使之成为艺术家可以张贴自己的混音提交 到目前为止,我已经设法得到的形式,要么使用作为特色图像上传的图像或给我的图像ID,这样我就可以把它在文章的内容,问题是我需要两者!我需要的图像自动设置为缩略图的职位,我需要的ID,以便我可以设置的形式,以自动把图像在我想要的地方张贴 到目前为止,我发现问题在于这段代码: if ($_FILES) { foreach ($_FILES as $file => $array) { $new
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id
}
}
我这样说是因为,无论我是否成功获取附件的ID,或者是否将图像设置为缩略图,此代码的位置都会发生变化
整个代码如下:
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// 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 );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
function mixtape_fep($content = null) {
global $post;
// We're outputing a lot of html and the easiest way
// to do it is with output buffering from php.
ob_start(); ?> <?php
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "new_post") {
// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
$title = $_POST['title'];
} else {
echo 'Please enter the wine name';
}
if (isset ($_POST['description'])) {
$description = $_POST['description'];
} else {
echo 'Please enter some notes';
}
$cover_id = get_post_meta($post->ID, 'thumb', true);
$cover = wp_get_attachment_link($cover_id);
$content = $cover.'<br /><br />'.$description;
$tags = $_POST['post_tags'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_category' => array($_POST['cat']), // Usable for custom taxonomies too
'tags_input' => array($tags),
'post_status' => 'publish', // Choose: publish, preview, future, draft, etc.
'post_type' => 'post' //'post',page' or use a custom post type if you want to
);
//SAVE THE POST
$pid = wp_insert_post($new_post);
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
// $newupload returns the attachment id of the file that
// was just uploaded. Do whatever you want with that now.
}
}
//SET OUR TAGS UP PROPERLY
wp_set_post_tags($pid, $_POST['post_tags']);
//REDIRECT TO THE NEW POST ON SAVE
$link = get_permalink( $pid );
wp_redirect( $link );
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action('wp_insert_post', 'wp_insert_post'); ?>
function insert\u attachment($file\u handler,$post\u id,$setthumb='false')){
//检查以确保上传成功
如果($\u FILES[$file\u handler]['error']!==UPLOAD\u ERR\u OK)\u返回\u 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\u id=media\u handle\u upload($file\u handler,$post\u id);
如果($setthumb)更新发布元($post\u id、“'u thumbnail\u id'、$attach\u id”);
返回$attach_id;
}
函数mixstape_fep($content=null){
全球$员额;
//我们输出了大量的html和最简单的方法
//要做到这一点,需要使用php的输出缓冲。
ob_start();?>通过在代码中添加以下内容,解决了分配缩略图和插入图像的问题:
if ( $_FILES ) {
$files = $_FILES['cover'];
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("cover" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
}
然后
set_post_thumbnail( $pid, $newupload );
在insert post函数下方。通过将以下内容添加到代码中,解决了分配缩略图和插入图像的问题:
if ( $_FILES ) {
$files = $_FILES['cover'];
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("cover" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post->ID);
}
}
}
}
然后
set_post_thumbnail( $pid, $newupload );
下面是插入post函数