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

问题描述 投票:0回答:1

我正在运行此功能:

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

它在提交时创建了一个帖子,但是如何添加在输入type =“ file”中选择的图像作为特色图像?

我也想用此前端表单填写ACF字段。

wordpress image post upload frontend
1个回答
0
投票

我认为您正在寻找set_post_thumbnail()。有一个很好的教程here,您可以将代码作为基础。

带有相关代码的节录:

<?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.';
    }
?>
© www.soinside.com 2019 - 2024. All rights reserved.