我需要通过ajax调用在Wordpress中设置一个图像。所以我做了这个功能:
function FeaturedImage() {
$image_ID = $_POST['thumbnail_id'];
$post_ID = $_POST['post_id'];
check_ajax_referer( 'savemediaimage', 'nonce' );
update_post_meta( $post_ID, '_thumbnail_id', $image_ID );
}
add_action( 'wp_ajax_postthumbnail', 'FeaturedImage' );
add_action( 'wp_ajax_nopriv_postthumbnail', 'FeaturedImage');
这是ajax电话:
jQuery.post(ajaxparam.ajax_url, {
nonce: ajaxparam.nonce,
post_id : '924',
thumbnail_id : '887',
action: 'postthumbnail'
});
在控制台日志中,发布内容为:
nonce=7bb3cb3f9d&post_id=924&thumbnail_id=887&action=postthumbnail
答案:
200 OK
但从来没有图像设置为特色。
我还尝试使用set_post_thumbnail
代替update_post_meta
但没有成功。
怎么了?感谢
答案 0 :(得分:0)
试试这个。它的工作
function postthumbnail() {
echo $image_ID = $_POST['thumbnail_id'];
$post_ID = $_POST['post_id'];
//check_ajax_referer( 'savemediaimage', 'nonce' );
update_post_meta( $post_ID, '_thumbnail_id', $image_ID );
}
add_action( 'wp_ajax_postthumbnail','postthumbnail' );
add_action( 'wp_ajax_nopriv_postthumbnail', 'postthumbnail');
<input type="button" id="test">
<script>
jQuery(document).on("click","#test",function(){
jQuery.ajax({
type: "POST",
url: ajaxparam.ajax_url,
data: {post_id : '9',thumbnail_id : '2850', action: 'postthumbnail', },
success: function(data) {
console.log(data);
alert('call back');
}
});
});
</script>