php - 如何从用户元中删除帖子/产品?
问题描述
在我的项目中有一个部分:最喜欢的。在帖子上,当您单击此按钮时,有一个按钮可以添加到收藏夹,帖子 ID 放在当前用户元数据上。这是按钮代码:
<span data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="receipt_favorites"></span>
这是ajax的Jquery:
jQuery('.receipt_favorites').on('click',function(e){
var postID = jQuery(this).attr("data-post-id");
var userID = jQuery(this).attr("data-user-id");
data = {
'action': 'dicaprio_add_fav_recipe',
'postID': postID,
'userID': userID
}
jQuery.ajax({
type:'post',
url: object_url.url,
data: data,
cache: false,
success: function(data) {
}
});
e.preventDefault();
});
这是php:
add_action( 'wp_ajax_nopriv_dicaprio_add_fav_recipe', 'dicaprio_add_fav_recipe' );
add_action( 'wp_ajax_dicaprio_add_fav_recipe', 'dicaprio_add_fav_recipe' );
function dicaprio_add_fav_recipe() {
$postID = $_POST['postID'];
$userID = $_POST['userID'];
$current_fav_list = get_user_meta( $userID, 'favorite_list_recept', true );
if ( is_array( $current_fav_list ) ) {
if ( in_array( $postID, $current_fav_list ) ) {
} else {
$current_fav_list[] = $postID;
}
} else {
$current_fav_list = array();
$current_fav_list[] = $postID;
}
update_usermeta( $userID, 'favorite_list_recept', $current_fav_list );
}
这三位一体做得很好,帖子被添加到收藏夹。但是如何从收藏夹中删除它?这是我的问题。我知道为此我需要创建一个按钮,例如:
<span data-post-id="<?php the_ID(); ?>" data-user-id="<?php echo $current_user->ID; ?>" class="remove__favore"></span>
并为 ajax 使用相同的 Jquery,仅通过此类激活:remove__favor。但是php函数部分呢?我认为这会更容易,但我无法编写删除最喜欢的帖子的函数。你能告诉我如何重新安排移除吗?我知道对于专业人士来说这是一个菜鸟问题,但是我花了很多时间却无法解决。
解决方案
推荐阅读
- python - 具有基本规则python的序列
- java - 在不使用另一个数组的情况下反转数组
- python - 字符串切片是否在内存中执行复制?
- css - 如何在 SVG 组件中按行对齐路径元素?
- javascript - casl中的项目级别权限
- html - 为记忆游戏创建网格
- magento2 - 在 Magento 2.3.4 中单击编辑产品时出现 ComponentType 错误
- react-native - 到达导航结束后反应本机重置导航
- cordova - 在ionic的相机预览插件的stopRecordVideo()函数返回的文件路径上找不到视频
- mysql - 表字段中的 MySQL 查询参数