ACF:检查关系并添加/删除
我正在尝试创建一个“书签”按钮……当用户添加书签帖子时,我将其保存到关系字段中。如果用户将其算上标记,请检查关系字段并将其从关系集合中删除。
添加很容易。事实证明,去除是屁股的痛苦,只是摧毁了整个数组:
$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
我假设由于数组已序列化,钥匙是错误的吗?我不能确定。
是否有更正式的方式检查当前的关系阵列,如果存在,则将其删除?
谢谢!
I’m trying to create a “bookmark” button of sorts … where when the user bookmarks a post, I save it to a Relationship field. If the user then unbookmarks it, I check the relationship field and remove it from the collection of relationships.
Adding is easy. Removing is proving to be a pain in the butt and just destroys the entire array:
$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
I'm assuming that because the array is serialized, the key is wrong? I can't be sure enough.
Is there a more official way of checking the current relationship array, and if it exists, removing it?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是答案:
Here is the answer: