为了账号安全,请及时绑定邮箱和手机立即绑定

从客串形式添加图像到post_content

从客串形式添加图像到post_content

PHP
牧羊人nacy 2022-09-12 10:55:45
我试图让客人通过帖子类别在我的wordpress页面中发布活动,他们需要为帖子设置帖子缩略图和1-3张图像。我添加了带有set_post_thumbnail()的帖子缩略图,但我无法弄清楚并在wordpress手札中找到一些东西来帮助我,也许你们知道一些东西可以帮助我走上正确的道路?到目前为止,我的代码://Upload Image    require_once( ABSPATH . 'wp-admin/includes/image.php' );    require_once( ABSPATH . 'wp-admin/includes/file.php' );    require_once( ABSPATH . 'wp-admin/includes/media.php' );    $attachment_id = media_handle_upload('file',$post_id);    $attachment_id2 = media_handle_upload('file2',$post_id);    $attachment_id3 = media_handle_upload('file3',$post_id);    $attachment_id4 = media_handle_upload('file4',$post_id);//Set Image as thumbnail    set_post_thumbnail($post_id, $attachment_id); 
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

我发现这个问题有点模糊,所以我会用我认为你的意思来回答。


首先,您需要表单上的属性。enctype


<form ... enctype="multipart/form-data">

    <input name="file" type="file">

    <input name="file2" type="file">

    <input name="file3" type="file">

    <input name="file4" type="file">

</form>

现在,在您的流程文件中,您需要从数组中获取每个文件。$_FILES


<?php


    $file = !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null;

    $file2 = !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null;

    $file3 = !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null;

    $file4 = !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null;

最后,您需要一个自定义函数来处理每个文件的上传。这是我在我的项目中创建和使用的那一个(针对此问题进行了修改,因此请进行测试!


<?php


function my_asset_uploader( $file, $parent_id = 0, $allowed = [] ) {

    require_once ABSPATH . 'wp-admin/includes/file.php';

    require_once ABSPATH . 'wp-admin/includes/image.php';


    // Get basic attributes

    $filename   = basename( $file[ 'name' ] );

    $mime       = wp_check_filetype( $filename );


    // Get the content type from response headers

    $type   = !empty( $mime[ 'type' ] ) ? $mime[ 'type' ] : $file[ 'type' ];

    $ext    = !empty( $mime[ 'ext' ] ) ? $mime[ 'ext' ] : trim( explode( '|' , array_search( $type, get_allowed_mime_types() ) )[ 0 ] );


    // Basic checks

    if ( !$type || !$ext || ( !empty( $allowed ) && is_array( $allowed ) && !in_array( $ext, $allowed ) ) ) {

        // Not a valid file

        return new WP_Error( 'upload', 'Invalid file type. Please try another file.' );

    }


    // Move file to wp-content

    $body   = @file_get_contents( $file[ 'tmp_name' ] );

    $file   = wp_upload_bits( $filename, null, $body );


    // Upload check

    if ( !empty( $file[ 'error' ] ) ) {

        return new WP_Error( 'upload', $file[ 'error' ] );

    }


    // Write attachment location to the database

    $attachment_id = wp_insert_attachment( [

        'post_title'        => $filename,

        'post_mime_type'    => $file[ 'type' ]

    ], $file[ 'file' ], $parent_id, true );


    if ( is_wp_error( $attachment_id ) ) {

        return $attachment_id;

    }


    // Generate meta

    $attach_data = wp_generate_attachment_metadata( $attachment_id, $file[ 'file' ] );

    wp_update_attachment_metadata( $attachment_id, $attach_data );


    return $attachment_id;

}

用法


<?php


/**

 * Extensions we allow the user to upload

 */

$allowed_extensions = [ 'jpg', 'jpeg', 'png', 'gif' ];


/**

 * Insert post

 */

$post_id = wp_insert_post( ... );


/**

 * Get all files from the request payload

 */

$all_images = array_filter( [

    !empty( $_FILES[ 'file' ] ) ? $_FILES[ 'file' ] : null,

    !empty( $_FILES[ 'file2' ] ) ? $_FILES[ 'file2' ] : null,

    !empty( $_FILES[ 'file3' ] ) ? $_FILES[ 'file3' ] : null,

    !empty( $_FILES[ 'file4' ] ) ? $_FILES[ 'file4' ] : null,

] );


/**

 * Uploaded files

 */

$attachment_ids = [];


/**

 * Check we have any images before looping

 */

if ( $all_images ) {

    foreach ( $all_images as $img_file ) {

        $this_id = my_asset_uploader( $img_file, $post_id, $allowed_extensions );


        if ( $this_id && !is_wp_error( $this_id ) ) {

            $attachment_ids[] = $this_id;

        }

    }

}


/**

 * Check at least one image was successfully uploaded

 */

if ( !empty( $attachment_ids[ 0 ] ) ) {

    set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );

}

这只是一个起点,引导您朝着正确的方向前进。


希望这有帮助!


编辑

将三个图像添加到post_content...


如果使用上述过程并且您拥有附件ID数组,则可以将每个附件的图像html附加到帖子内容中,如下所示:


<?php


...


/**

 * Check at least one image was successfully uploaded

 */

if ( !empty( $attachment_ids[ 0 ] ) ) {

    set_post_thumbnail( $post_id, $attachment_ids[ 0 ] );

}


/**

 * We have multiple files, lets append them to the post_content

 */

if ( count( $attachment_ids ) > 1 ) {


    $appendage = '';


    foreach ( $attachment_ids as $i => $img_id ) {

        // Skip the first image because we used that as the thumbnail

        if ( $i === 0 ) continue;


        // Get the attachment HTML for a `large` sized image

        if ( $html = wp_get_attachment_image( $img_id, 'large' ) ) {

            $appendage .= $html;

        }

    }


    if ( !empty( $appendage ) ) {


        /**

         * Get the current post content

         */

        $current_content = get_post_field( 'post_content', $post_id, 'raw' );


        /**

         * Update the post

         */

        wp_update_post( [

            'ID' => $post_id,

            'post_content' => ( $current_content . $appendage ) // Joining the two strings

        ] );

    }


}


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号