简体   繁体   English

WordPress:ACF将行添加到转发器字段

[英]WordPress: ACF add rows to a repeater field

I'm looking for a way to add rows to a repeater field in ACF Pro. 我正在寻找一种在ACF Pro中向行转发器字段添加行的方法。 I found this post but the solution post doesn't seem to work. 我发现这篇文章,但解决方案的帖子似乎不起作用。 I will describe my problem: 我会描述我的问题:

I have a custom post type called "gebruikers", a repeater field called "logins" and a rows that can have a field called "datum". 我有一个名为“gebruikers”的自定义帖子类型,一个名为“logins”的转发器字段和一个可以有一个名为“datum”的字段的行。 I would like to be able to add a value to a new row in the field "datum". 我希望能够在“datum”字段中为新行添加值。 Is this possible? 这可能吗?

My code so far: 我的代码到目前为止:

$field_key = "logins";
$user_id = "gebruiker_23";
$value = get_field($field_key, $user_id);
$value[] = array('date' => date('Y-m-d H:i:s'));
update_field( $field_key, $value, $user_id );*/

I think the add_row function is what you are looking for. 我认为add_row函数就是你要找的东西。 https://www.advancedcustomfields.com/resources/add_row/ https://www.advancedcustomfields.com/resources/add_row/

$row = array(
 'datum' => 'new value',
); 
$i = add_row( 'logins', $row );

Finally figured this out! 终于搞清楚了!

Change $field_key = "logins"; 更改$ field_key =“登录”; to the field key that is in the Wordpress posts table. 到Wordpress帖子表中的字段键。 Grab the field_id from there (post_name column) that is assigned to your main repeater field. 从那里(post_name列)获取分配给主转发器字段的field_id。

Use the ID from post_name. 使用post_name中的ID。

Now you can insert to these records if none already exist. 现在您可以插入这些记录(如果不存在)。

Example that worked for me on a user record with no previous rows: 在没有前一行的用户记录上为我工作的示例:

$field_key = "field_5657dffe586cc";
$value = get_field($field_key, 'user_24');
$value[] = array("career_enjoyment_id" => "Foo", "career_enjoyment_file" => "Bar");
update_field( $field_key, $value, 'user_24' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM