从前端特色图片和ACF创建WordPress帖子

从前端特色图片和ACF创建WordPress帖子,wordpress,image,post,upload,frontend,Wordpress,Image,Post,Upload,Frontend,我正在运行此功能: function create_post() { ob_start(); if(isset($_POST['new_post']) == '1') { $post_title = $_POST['post_title']; $post_category = $_POST['cat']; $post_content = $_POST['post_content']; $new_post = array( 'ID' =>

我正在运行此功能:

function create_post() {
ob_start();
if(isset($_POST['new_post']) == '1') {
    $post_title = $_POST['post_title'];
    $post_category = $_POST['cat'];
    $post_content = $_POST['post_content'];

    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_category' => array($post_category),
          'post_content' => $post_content, 
          'post_title' => $post_title,
          'post_status' => 'publish'
        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $post = get_post($post_id);
    wp_redirect($post->guid);
}      
?>      

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title" placeholder="Voer hier de titel van het nieuwsbericht in"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc" placeholder="Voer hier de tekst van het bericht in"></textarea>
    <p>
    Kies hieronder de afbeelding van het nieuwsbericht. 
    </p>
    <input type="file" id="featured_image" name="featured_image" accept="image/*">
    <br/><br/>
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Nieuwsbericht aanmaken"/>
</form>

<?php
return ob_get_clean();
}
add_shortcode('create_post', 'create_post');
它会在提交时创建一个帖子,但如何将输入类型=文件中选择的图像添加为特色图像


另外,我想用这个前端表单填写ACF字段。

我想您正在寻找set\u post\u缩略图。有一个很好的教程,您可以根据它编写代码

相关代码摘录:

<?PHP
//prepare upload image to WordPress Media Library
    $upload = wp_upload_bits( $IMGFileName, null, file_get_contents( $IMGFilePath, FILE_USE_INCLUDE_PATH ) );
// check and return file type
    $imageFile  = $upload['file'];
    $wpFileType = wp_check_filetype( $imageFile, null );
// Attachment attributes for file
    $attachment = array(
        'post_mime_type' => $wpFileType['type'],  // file type
        'post_title'     => sanitize_file_name( $imageFile ),  // sanitize and use image name as file name
        'post_content'   => '',  // could use the image description here as the content
        'post_status'    => 'inherit'
    );
// insert and return attachment id
    $attachmentId = wp_insert_attachment( $attachment, $imageFile, $postId );
// insert and return attachment metadata
    $attachmentData = wp_generate_attachment_metadata( $attachmentId, $imageFile );
// update and return attachment metadata
    wp_update_attachment_metadata( $attachmentId, $attachmentData );
// finally, associate attachment id to post id
    $success = set_post_thumbnail( $postId, $attachmentId );
// was featured image associated with post?
    if ( $success ) {
        $message = $IMGFileName . ' has been added as featured image to post.';
    } else {
        $message = $IMGFileName . ' has NOT been added as featured image to post.';
    }
?>

如果我想将$IMGFilePath上传到媒体库中,它应该是什么路径$IMGFileName=$\u POST[featured\u image]?@LTCode如果您没有阅读完整的教程,那么您应该阅读完整的教程及其参考资料。您需要将enctype=multipart/form数据添加到表单中$IMGFileName可能应该是$\u FILES[featured\u image][filename]。