1

我有一个为我的网站制作的 WP 前端发布表单,我的想法是让艺术家可以发布他们自己的混音带提交!

到目前为止,我已经设法让表单使用上传的图片作为特色图片,或者给我图片 ID,这样我就可以把它放在帖子内容中,问题是我两者都需要!我需要将图像自动设置为帖子的缩略图,并且我需要 ID,以便我可以将表单设置为自动将图像放在帖子中我想要的位置!

到目前为止,我已经将问题定位为这段代码:

     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'); ?>
4

1 回答 1

1

通过将其添加到代码中来解决分配缩略图和插入图像的问题:

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 );

在插入帖子功能下方。

于 2013-10-02T19:56:38.467 回答