【问题标题】:set-post-thumbnail wp-ajax return 0设置后缩略图 wp-ajax 返回 0
【发布时间】:2016-04-28 12:30:37
【问题描述】:

我尝试使用 wordpress 的媒体管理器,我使用管理员 wordpress 之外的帖子编辑器,用户可以创建带有特色图片的帖子。 我使用 _wp_post_thumbnail_html 函数来显示图片或显示上传文件的链接,所有具有“发布者”角色的用户都可以上传图片和上传作品,但不能显示特色图片或分配给帖子。

关于 wp-ajax whit 操作:set-post-thumbnail 返回 0 并且图像未分配给新帖子。 wp-ajax.php:

json:true thumbnail_id:3952 _wpnonce:b02e8553f1 action:set-post-thumbnail

响应:0

我的代码为:

<?php $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
                        //$postid =  get_post( $post_id );
                        echo _wp_post_thumbnail_html( $thumbnail_id, $post_id );
                    ?>
                    <br>

非常简单,从 wordpres 显示媒体管理器,允许上传特色图片但不允许分配给新帖子。有什么解决办法吗?

编辑:在编辑帖子中工作正常允许更改特色图片我想是因为特色图片需要帖子 ID,但在来自 wordpress 的新帖子上允许上传图片并将其分配给新帖子。

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    这是正确的图像上传代码。

    <?php
    global $wpdb;
    include ('../../../../wp-load.php');
    if ( $_FILES ) {
    
        if(!function_exists('wp_handle_upload')){
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
    
    
    
        $upload_overrides = array('test_form' => false);
    
        $response = array();
        ini_set('max_execution_time', 0);
        foreach($_FILES as $file){
    
            $movefile_profile = wp_handle_upload($file, $upload_overrides);
            $filename = $movefile_profile['url'];
            $parent_post_id = $post_id; // please provide post id must
            $filetype = wp_check_filetype( basename( $filename ), null );
            $wp_upload_dir = wp_upload_dir();
            $attachment = array(
                'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                'post_mime_type' => $filetype['type'],
                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                'post_content'   => '',
                'post_status'    => 'inherit'
            );
    
            $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
            $filepath = $wp_upload_dir['path'] . '/' . basename( $filename );
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );
            wp_update_attachment_metadata( $attach_id, $attach_data );
            set_post_thumbnail($post_id, $attach_id); // this set_post_thumbnail will set you post thumbnail
            $response['message'] = 'Done';
        }
    
        echo json_encode($response);
        die();
    
    }
    

    对于 Ajax 代码,请访问 link。如果您有任何问题,请在下方评论。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多