因此,我在自定义帖子类型(产品)中生成了一个Repeater字段,但是当我尝试保存一些数据并且页面刷新时,它不会保存。
在acf forum中有一个代码,您可以在其中保存中继器字段值,如下所示:
// save a repeater field value
$field_key = "field_12345678";
$value = array(
array(
"sub_field_1" => "Foo",
"sub_field_2" => "Bar"
)
);
update_field( $field_key, $value, $post_id );
但是我不确定在我的情况下将$value
字段拉到哪里。
这是我的代码:
function wpt_add_product_metaboxes() {
global $post;
$unique_terms = array();
$postID = $post->ID;
$have_terms = get_the_terms($postID, 'product_attributes');
if($have_terms):
$parent_terms = wp_get_post_terms( $postID,'product_attributes', array( 'parent' => 0, 'orderby' => 'id', 'order' => 'DESC', 'hide_empty' => false ) );
#print("<pre>".print_r($parent_terms,true)."</pre>");
$ctr = 0;
foreach ($parent_terms as $p_term):
$custom_taxnomies_child = wp_get_post_terms($postID, 'product_attributes', array( 'parent' => 66, 'orderby' => 'slug', 'hide_empty' => false ));
foreach($custom_taxnomies_child as $cterm){
if( ! in_array( $cterm->term_id, $unique_terms ) ){
array_push( $unique_terms, $cterm->term_id );
$ctr++;
acf_add_local_field( array (
'key' => 'field_tab_size_'.$ctr,
'label' => $cterm->name,
'name' => 'store_sizes_'.$ctr,
'type' => 'tab',
'parent' => 'field_5bd14c9349930',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
));
/**
* Initial Repeater Field
*
*/
acf_add_local_field( array (
'key' => 'field_product_sizes_prices'.$ctr,
'label' => 'Product Sizes and Prices',
'name' => 'product_sizes_'.$ctr,
'type' => 'repeater',
'parent' => 'field_5bd14c9349930',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add Row'
));
/**
* Add Start Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_total_products_'.$ctr,
'label' => 'Total Products',
'name' => 'total_products'.$ctr,
'parent' => 'field_product_sizes_prices'.$ctr, // key of parent repeater
'type' => 'text',
'instructions' => '',
));
/**
* Add End Date Subfield
* */
acf_add_local_field( array (
'key' => 'field_total_prices_'.$ctr,
'label' => 'Total Prices',
'name' => 'total_prices',
'parent' => 'field_product_sizes_prices'.$ctr,
'type' => 'text',
));
}
endforeach;
endif;
}