增加值而不是使用WordPress更新user_meta(Increase value instead of updating user_meta with WordPress)
所以,我使用以下代码来允许人们将步兵或车辆添加到他们的个人资料中
function after_submission($form) { $user = wp_get_current_user(); $user_id = $user->ID; $infantry = $form[1]; $vehicle = $form[2]; update_user_meta( $user_id, 'infantry', $infantry); update_user_meta( $user_id, 'vehicle', $vehicle);我的问题是步兵和车辆正在更新,而不是增加。
有什么建议么?
So, I'm using the following code to allow people to add infantry or vehicle to their profile
function after_submission($form) { $user = wp_get_current_user(); $user_id = $user->ID; $infantry = $form[1]; $vehicle = $form[2]; update_user_meta( $user_id, 'infantry', $infantry); update_user_meta( $user_id, 'vehicle', $vehicle);My problem is that infantry & vehicle is getting updated, rather than increased.
Any suggestions?
最满意答案
试试这样:
$user = wp_get_current_user(); $user_id = $user->ID; $infantry = $form[1]; $vehicle = $form[2]; $oldInfantry = get_user_meta( $user_id, 'infantry', true); $oldVehicle = get_user_meta( $user_id, 'vehicle', true); update_user_meta( $user_id, 'infantry', $infantry+$oldInfantry); update_user_meta( $user_id, 'vehicle', $vehicle+$oldVehicle);try like this:
$user = wp_get_current_user(); $user_id = $user->ID; $infantry = $form[1]; $vehicle = $form[2]; $oldInfantry = get_user_meta( $user_id, 'infantry', true); $oldVehicle = get_user_meta( $user_id, 'vehicle', true); update_user_meta( $user_id, 'infantry', $infantry+$oldInfantry); update_user_meta( $user_id, 'vehicle', $vehicle+$oldVehicle);更多推荐
发布评论