【发布时间】:2012-01-24 13:45:43
【问题描述】:
我有一个自定义帖子类型,我们称之为产品。当用户将此产品拖到购物车(可放置的 jQuery UI)时,我希望自定义帖子类型中名为“金额”的键减少 1。
到目前为止,我通过 jQuery $.ajax 有一个 JSON 函数,如下所示:
$.ajax({ url: 'http://localhost:8888/MAMP/nogg/wordpress/wp-content/themes/twentyeleven/functions.php',
data: { postid: +id },
type: 'post',
success: function(output) {
alert("amount is reduced by 1.");
}
});
这会将帖子的ID发送到functions.php,然后我用它来获取我的functions.php中的数据
if(isset($_POST['postid']) && !empty($_POST['postid'])) {
$postid = $_POST['postid'];
$response = json_decode($postid);
remove_amount($response);
}
使用 postid 调用函数。
function remove_amount($postid) {
$amount = get_post_meta($postid, 'amount', true);
update_post_meta($postid, 'amount', $amount--);
}
这给了我一个 500 错误,我确保它是已发送的正确 ID,并检查了包含密钥(金额)的字段的名称。
那么我的愚蠢自我在这里缺少什么?
【问题讨论】: