ACF:检查关系并添加/删除

发布于 2025-02-08 01:10:14 字数 544 浏览 2 评论 0原文

我正在尝试创建一个“书签”按钮……当用户添加书签帖子时,我将其保存到关系字段中。如果用户将其算上标记,请检查关系字段并将其从关系集合中删除。

添加很容易。事实证明,去除是屁股的痛苦,只是摧毁了整个数组:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-02-15 01:10:14

这是答案:

unset($current_bookmarks[$key]);
    
$updated_bookmarks = $current_bookmarks;

Here is the answer:

unset($current_bookmarks[$key]);
    
$updated_bookmarks = $current_bookmarks;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文