【问题标题】:Create WordPress post from frontend featured image and ACF从前端特色图像和 ACF 创建 WordPress 帖子
【发布时间】:2020-06-04 21:54:13
【问题描述】:

我正在运行这个函数:

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】:

    我认为您正在寻找 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.';
        }
    ?>
    

    【讨论】:

    • 如果我想将$IMGFilePath 上传到我的媒体库中,它应该是什么路径? $IMGFileName = $_POST[featured_image]?
    • @LTCode 如果您还没有阅读整个教程及其参考资料(特别是 w3schools.com/php/php_file_upload.asp),您应该阅读整个教程。您需要将 enctype="multipart/form-data" 添加到表单中。 $IMGFileName 应该是 $_FILES["featured_image"]["filename"]。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2021-12-26
    • 2018-09-21
    相关资源
    最近更新 更多