我需要从我的表单添加附加缩略图

时间:2012-03-05 作者:Mohannad Qamara

我需要在此表单中添加[附加缩略图]

功能。php代码:

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    $title =  $_POST[\'title\'];
    $ordertype = $_POST[\'ordertype\'];
    $price = $_POST[\'price\'];
    $ordertime = $_POST[\'ordertime\'];

    // Add the content of the form to $post as an array
    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_category\' => array($_POST[\'category\']),
        \'post_status\'   => \'publish\',
        \'post_type\' => \'post\'
    );
    //save the new post
    $pid = wp_insert_post($new_post); 
    add_post_meta($pid, \'ordertype\', $ordertype, true);
    add_post_meta($pid, \'price\', $price, true);
    add_post_meta($pid, \'ordertime\', $ordertime, true);
    wp_redirect(\'/\'); exit;
    //insert taxonomies
}
资料来源:Form to Add Posts to Custom Post Type

2 个回复
SO网友:ifdion

其实很简单。下面是set\\u缩略图函数参考的链接codex. 以下是file upload.

首先,您需要将文件附加到帖子:

function attach_uploads($uploads,$post_id = 0){
$files = rearrange($uploads);
if($files[0][\'name\']==\'\'){
    return false;   
}
foreach($files as $file){
    $upload_file = wp_handle_upload( $file, array(\'test_form\' => false) );
    $attachment = array(
    \'post_mime_type\' => $upload_file[\'type\'],
    \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($upload_file[\'file\'])),
    \'post_content\' => \'\',
    \'post_status\' => \'inherit\'
);
    $attach_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
    $attach_array[] = $attach_id;
    require_once(ABSPATH . \'wp-admin/includes/image.php\');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file[\'file\'] );
    wp_update_attachment_metadata( $attach_id, $attach_data );
}
return $attach_array;
}
有关php文件上载重排的更多详细信息:php.net.

这是将上传的文件设置为后期缩略图的功能。将其放入相应的wp\\u ajax函数中。

// put this one early, before any process
$files = $_FILES[\'profile-picture\'];

// insert attachment, after you have the new post id of course
$attached_files = attach_uploads($files,$pid);

// set the first file as post thumbnail
// $attached_files[0] is for the first file

if($attached_files){
    set_post_thumbnail( $pid, $attached_files[0] ); 
}
希望这有帮助

ADDED :

function rearrange( $arr ){
    foreach( $arr as $key => $all ){
        foreach( $all as $i => $val ){
            $new[$i][$key] = $val;    
        }    
    }
    return $new;
}
把它放在函数里就行了。php

SO网友:Mohannad Qamara

谢谢@ifdion。。这就是我的最终功能。PHP代码

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_post") {

    // Do some minor form validation to make sure there is content
    $title =  $_POST[\'title\'];
    $ordertype = $_POST[\'ordertype\'];
    $price = $_POST[\'price\'];
    $ordertime = $_POST[\'ordertime\'];
    $files = $_FILES[\'profile-picture\'];

    // Add the content of the form to $post as an array
    $new_post = array(
        \'post_title\'    => $title,
        \'post_content\'  => $description,
        \'post_category\' => array($_POST[\'category\']),  // Usable for custom taxonomies too
        \'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 new post
    $pid = wp_insert_post($new_post); 
    add_post_meta($pid, \'ordertype\', $ordertype, true);
    add_post_meta($pid, \'price\', $price, true);
    add_post_meta($pid, \'ordertime\', $ordertime, true);
    $attached_files = attach_uploads($files,$pid);
    if($attached_files){
    set_post_thumbnail( $pid, $attached_files[0] ); 
    }
    wp_redirect(get_permalink($pid)); exit;
    //insert taxonomies
}
function attach_uploads($uploads,$post_id = 0){
    $files = rearrange($uploads);
    if($files[0][\'name\']==\'\'){
        return false;   
    }
    foreach($files as $file){
        $upload_file = wp_handle_upload( $file, array(\'test_form\' => false) );
        $attachment = array(
        \'post_mime_type\' => $upload_file[\'type\'],
        \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\', basename($upload_file[\'file\'])),
        \'post_content\' => \'\',
        \'post_status\' => \'inherit\'
    );
        $attach_id = wp_insert_attachment( $attachment, $upload_file[\'file\'], $post_id );
        $attach_array[] = $attach_id;
        require_once(ABSPATH . \'wp-admin/includes/image.php\');
        $attach_data = wp_generate_attachment_metadata( $attach_id, $upload_file[\'file\'] );
        wp_update_attachment_metadata( $attach_id, $attach_data );
    }
    return $attach_array;
}
并在主题中上载[输入]。。

<input type="file" id="profile-picture" name="profile-picture[]" size="40" multiple />
但如果我提交表单,并用此错误进行响应。。。

致命错误:调用未定义的函数重排()

抱歉占用您的时间

结束

相关推荐

CForms插件函数“my_cForms_logic”(用于编写自定义逻辑)正在运行5次

我正在使用cforms插件,它提供了一种使用文件my functions为表单操作处理程序页面编写自定义函数的方法。php和函数my\\u cforms\\u logic(。问题是,每当我提交表单时,该函数都运行得很好,但它运行了5次。例如,如果我在函数中编写一条语句,如echo“a”;然后在输出中我看到“AAAAA”。