【发布时间】:2019-10-04 14:37:50
【问题描述】:
我正在提交更新 Wordpress ACF field data using the update_field function 的表单。这很好用,但我遇到的问题是数据已提交,它会将其添加到 Wordpress 中正确的 ACF 字段,但我随后手动刷新页面以查看前端的新数据。
如何提交数据然后在第一次重新加载时显示?
<form action="#" class="forum-post-comment-form" id="forum-comment-<?php echo $counter; ?>" method="get">
<input type="text" placeholder="Type your answer here" class="forum-comment-box" id="forum-comment-<?php echo $counter; ?>" name="forum-comment-<?php echo $counter; ?>">
<span><input type="submit" class="comment-submit-btn" name="submit-<?php echo $counter; ?>" value=""></span>
</input>
</form>
<?php
$value = array();
if( have_rows( 'comments', $postid ) ):
while( have_rows( 'comments', $postid ) ): the_row();
$time_posted = get_sub_field( 'time', $postid );
$posted = get_sub_field( 'posted_by', $postid );
$comment = get_sub_field( 'comment', $postid );
$answerresponse = get_sub_field( 'answer', $postid );
$value[] = array(
"time" => $time_posted,
"posted_by" => $posted,
"comment" => $comment,
"answer" => $answerresponse
);
endwhile;
endif;
$submit = "submit-" . $counter;
if(isset($_POST[$submit])){
$form = "forum-comment-" . $counter;
$finalanswer = "answer-" . $counter;
$finalanswer = $_POST[$finalanswer];
$comment = $_POST[$form];
//if ($comment != "") {
$time_posted = date('Y-m-d H:i:s', time());
// Comments
$postid = get_the_id();
$field_key = "field_5c90d272c7ca9";
$value[] = array(
"time" => $time_posted,
"posted_by" => $current_user->ID,
"answer" => $finalanswer,
"comment" => $comment,
);
update_field( $field_key, $value, $postid );
//}
}
?>
ACF 字段布局:
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5c90d20a076ab',
'title' => 'Comments',
'fields' => array(
array(
'key' => 'field_5c90d272c7ca9',
'label' => 'Comments',
'name' => 'comments',
'type' => 'repeater',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => 0,
'max' => 0,
'layout' => 'block',
'button_label' => 'Add Comment',
'sub_fields' => array(
array(
'key' => 'field_5c90f06883914',
'label' => 'Time',
'name' => 'time',
'type' => 'date_time_picker',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'display_format' => 'Y-m-d H:i:s',
'return_format' => 'Y-m-d H:i:s',
'first_day' => 1,
),
array(
'key' => 'field_5c90ee4fc7cac',
'label' => 'Posted by',
'name' => 'posted_by',
'type' => 'user',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'role' => '',
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'array',
),
array(
'key' => 'field_5c90d38cc7cab',
'label' => 'Answer?',
'name' => 'answer',
'type' => 'text',
'instructions' => 'Change to "Yes" to mark as the answer',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '33',
'class' => '',
'id' => '',
),
'default_value' => 'No',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
array(
'key' => 'field_5c90d28ac7caa',
'label' => 'Comment',
'name' => 'comment',
'type' => 'textarea',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '100',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'maxlength' => '',
'rows' => '',
'new_lines' => '',
),
),
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'forum',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => array(
0 => 'excerpt',
1 => 'discussion',
2 => 'comments',
3 => 'slug',
4 => 'author',
5 => 'format',
6 => 'page_attributes',
7 => 'featured_image',
8 => 'categories',
9 => 'tags',
10 => 'send-trackbacks',
),
'active' => true,
'description' => '',
));
endif;
【问题讨论】:
-
你试过类似
$value = get_field( $field_key ); $value[] = [ ... new data ... ]; update_field( $field_key, $value ); $value = get_field( $field_key );的东西吗? -
@Rob,“在此之上” - 对不起,“这个”是什么?表格?或者您是否显示来自
$value数组的数据?这意味着在表格的正下方?您能否导出您的 ACF 字段设置,以便我们(希望)重现该问题?或者comments是转发器还是灵活的内容字段? (抱歉有很多问题..) -
感谢您的澄清,@Rob。那么在调用
update_field()之后,还需要对$value数组做些什么吗? -
不,我很确定这不是缓存问题。
$counter是什么?它是如何/在哪里定义的? -
好的,@Rob。我(想我)实际上知道发生了什么以及应该做什么。我会尽快发布答案。
标签: php advanced-custom-fields