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 に答える