Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中 [英] Wordpress ACF: How to add rows to a repeater field attached to a user via custom code (PHP)

查看:76
本文介绍了Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看过ACF和ACF Repeater Field文档,但让我有些困惑。

I have been through the ACF and ACF Repeater Field documentation but it has left me a little bit confused.

我有很多用户,并且我拥有(通过ACF)在每个中继器字段上都添加了一个中继器字段(称为事件),该中继器字段具有一个称为事件ID(event_id)的子字段。

I have a bunch of users and I have (through ACF) attached a repeater field to each of them (called Events), the repeater field has a sub-field called Event ID (event_id).

现在我面临的困境是我希望能够根据用户尝试添加和删除的事件向用户添加和删除event_id行。

Now the dilemma I have is that I want to be able to add and remove rows of event_id from the user depending on which events they have tried to add and remove.

我知道 update_field($ field_key,$ value,$ post_id)函数,但是我不太确定如何利用它来将行添加到附加到用户的重复器字段的子字段中。我还需要一些有关如何删除项目的指导。

I know of the update_field($field_key, $value, $post_id) function but I'm not too sure how to utilise it to add rows into sub-fields of a repeater field attached to a user. I also need some direction as to how to remove items.

我们将提供任何帮助!

推荐答案

要将行添加到转发器字段的子字段中,您可以执行以下操作:

To add rows into sub-fields of a repeater field, you can do:

$field_key = "repeater_field";
$user_id = "user_123"; // save to user (user id = 123)

$value = get_field($field_key, $user_id);
$value[] = array("event_id " => 25);
$value[] = array("event_id " => 30);
update_field( $field_key, $value, $user_id );

要删除项目,我会使用PHP函数,例如< a href = https://stackoverflow.com/questions/4466159/delete-element-from-multiDimension-array-based-on-value>根据值从多维数组中删除元素:

To remove items, I'd use a PHP function such as the one in the accepted answer at Delete element from multidimensional-array based on value:

$value = removeElementWithValue($value, "event_id", 25);
update_field( $field_key, $value, $user_id );

根据 where 来添加这些代码(即动作挂钩)取决于何时您要添加/删除项目。

As to where to add these codes (ie. the action hook) depends on when you'd like to add/remove the items.

这篇关于Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆