wp_update_post缩略图id wordpress

时间:2016-04-13 08:32:13

标签: wordpress image edit featured

我正在尝试从前端添加/编辑帖子!我在编辑动作中编辑缩略图有问题,标题和描述正在更新但缩略图不是?这是我的代码:

    <?php
    /**
    * Template Name: Edit Posts
    */


    if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] )         && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {

    $postID = $_GET['pos_id'];

$post_information = array(
'ID' => $postID,
'post_title' =>  wp_strip_all_tags( $_POST['postTitle'] ),
'post_content' => $_POST['postContent'],
'post_type' => 'product',
'post_status' => 'pending'
);

$post_id = wp_update_post( $post_information );

// Upload Featured

$filename = $_FILES['thumbnail']['name'];
$wp_filetype = wp_check_filetype( basename($filename), null );
$wp_upload_dir = wp_upload_dir();

// Move the uploaded file into the WordPress uploads directory
move_uploaded_file( $_FILES['thumbnail']['tmp_name'], $wp_upload_dir['path']  . '/' . $filename );

$attachment = array(
    'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), 
    'post_mime_type' => $wp_filetype['type'],
    'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
    'post_content' => '',
    'post_status' => 'inherit'
);

$filename = $wp_upload_dir['path']  . '/' . $filename;

$attach_id = wp_insert_attachment( $attachment, $filename, 37 );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta($post_id, '_thumbnail_id', $attach_id);

}

    get_header();


if ( isset( $_GET['pos_id'] ) ) { 






?>                                      

<?php $query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => '-1', 'post_status' => array('publish','pending','draft', 'private', 'trash') ) ); ?>

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query-  >the_post(); ?>


   <?php  



 if ( $_GET['pos_id'] == $post->ID )
{
    $current_post = $post->ID;
    $title = get_the_title();
    $content = get_the_content();
    echo $post->ID;


?> 



 <form action="" id="primaryPostForm" method="POST">

<fieldset>

    <label for="postTitle"><?php _e( 'Post\'s Title:', 'framework' ); ?>       </label>

    <input type="text" name="postTitle" id="postTitle" value="<?php echo get_the_title(); ?>" class="required" />

</fieldset>

<?php if ( $postTitleError != '' ) { ?>
    <span class="error"><?php echo $postTitleError; ?></span>
    <div class="clearfix"></div>
<?php } ?>



    <!-- images -->
<?php echo get_the_post_thumbnail( $post_id, array( 200, 150 ) ); ?><br /> 
    <input type="file" name="thumbnil" id="thumbnil" />


<fieldset>

    <label for="postContent"><?php _e( 'Post\'s Content:', 'framework' ); ?></label>

    <textarea name="postContent" id="postContent" rows="8" cols="30"><?php echo $content; ?></textarea>

</fieldset>

<fieldset>

    <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
    <input type="hidden" name="submitted" id="submitted" value="true" />
    <button type="submit"><?php _e( 'Update Post', 'framework'); ?></button>

</fieldset>

 </form>
 <?php 

 }




 ?> 
 <?php endwhile; endif; ?>
 <?php 

 wp_reset_query(); 


}else{ 

echo 'No posts found'; 

}

?>

    <?php
    get_footer();

 ?>

任何想法如何解决?提前谢谢!

0 个答案:

没有答案