Support

Account

Home Forums Add-ons Flexible Content Field Load flexible content field from setting page

Solved

Load flexible content field from setting page

  • Hi Guys,

    I have created a settings page, in it I have a flexible content field with 2 sub fields.
    On a Post I have another flexible content field, identical to my settings page, I am trying to take the values entered on the setting page and have them populate on the post as “default values”.

    Here is the code I am trying, but its not working, it keeps creating empty fields on my post and messes up the field group also!

    function acf_load_deliverables( $field ) {
    	$field = array();
    		
    		if( have_rows('deliverables', 'option' )){
    			
    			// loop through the rows of data
    			while ( have_rows('deliverables', 'option') ) : the_row();
    			
    				if( get_row_layout() == "new_deliverable" ){
    					
    					$field = array(
    						array(
    						  'acf_fc_layout' => 'development_deliverables',
    						  'deliverables' => get_sub_field('deliverables'),
    						  'days' => get_sub_field('days')
    						));
    				}
    				else {
    					echo "no rows";
    					echo "<br />";
    				}
    			endwhile;
    		}
    		else
    			echo "no rows"; 
        
        return $field;
    }
    add_filter('acf/load_field/key=field_5d0b74c37ad29', 'acf_load_deliverables');

    Does anyone have an example of this working I could reference?

  • I got this working, here is the code encase anybody has the same problem:

    function copy_Dev_settings( $field ) {

    if ( is_edit_page(‘new’) ) { // only populate for new posts
    //echo “this is a new post!! <br />”;

    if( have_rows(‘deliverables’, ‘option’ )){ // if there are values on the settings page for this option
    $value = array();

    while ( have_rows(‘deliverables’, ‘option’) ) : the_row(); //starting at a specific row
    if( get_row_layout() == “new_deliverable” ){ //set the layout for the flexible content field

    $acf_fc_layout = ‘new_deliverable’; //set the values for layout and each sub field
    $deliv = get_sub_field(‘deliverables’);
    $days = get_sub_field(‘days’);

    if ( $acf_fc_layout == ‘new_deliverable’ ) { // apply the values to the correct layout and fields using the key value for that sub field
    $value[] = array(
    ‘acf_fc_layout’ => $acf_fc_layout,
    ‘field_5d0c21f3e31e1’ => $deliv,
    ‘field_5d0c220ce31e2’ => $days
    );
    }
    }
    else {
    echo “cannot find development deliverables row layout”;
    //echo “<br />”;
    }

    endwhile;

    $field[‘value’] = $value; //assign the values to the field value array
    }
    else
    echo “nothing”;

    }

    return $field; //return the full field array to the page/post etc

    }
    add_filter(‘acf/load_field/key=field_5d0b74c37ad29’, ‘copy_Dev_settings’);

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Load flexible content field from setting page’ is closed to new replies.