我正在尝试创建一个"书签"按钮,当用户添加书签时,我将其保存到一个关系字段。如果用户随后取消了它的书签,我检查关系字段并将其从关系集合中删除。
添加很容易。删除被证明是一个痛苦的屁股,只是破坏整个数组:
$current_bookmarks = get_field( 'webinar_bookmarks', 'user_' . get_current_user_id() );
if ( in_array($webinar_post, $current_bookmarks) ) : // bookmarked, remove
$key = array_search($webinar_post, $current_bookmarks);
unset($current_bookmarks, $key);
else : // not bookmarked, add
我假设因为数组是序列化的,键是错误的?我不能肯定。
是否有更正式的方法来检查当前的关系数组,如果它存在,删除它?
谢谢!
答案如下:
unset($current_bookmarks[$key]);
$updated_bookmarks = $current_bookmarks;