wp_ajax_set_post_thumbnail

函数
wp_ajax_set_post_thumbnail ( No parameters )

wp_ajax_set_post_thumbnail:此函数处理ajax请求,将附件设置为post缩略图。它接受附件ID和文章ID作为参数,并将附件设置为文章缩略图。

设置特色图片的Ajax处理程序。

function wp_ajax_set_post_thumbnail() {
	$json = ! empty( $_REQUEST['json'] ); // New-style request.

	$post_ID = (int) $_POST['post_id'];
	if ( ! current_user_can( 'edit_post', $post_ID ) ) {
		wp_die( -1 );
	}

	$thumbnail_id = (int) $_POST['thumbnail_id'];

	if ( $json ) {
		check_ajax_referer( "update-post_$post_ID" );
	} else {
		check_ajax_referer( "set_post_thumbnail-$post_ID" );
	}

	if ( '-1' == $thumbnail_id ) {
		if ( delete_post_thumbnail( $post_ID ) ) {
			$return = _wp_post_thumbnail_html( null, $post_ID );
			$json ? wp_send_json_success( $return ) : wp_die( $return );
		} else {
			wp_die( 0 );
		}
	}

	if ( set_post_thumbnail( $post_ID, $thumbnail_id ) ) {
		$return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
		$json ? wp_send_json_success( $return ) : wp_die( $return );
	}

	wp_die( 0 );
}

常见问题

FAQs
查看更多 >