Support

Account

Home Forums ACF PRO Get all Texts form Flexible Contents and nested Repeater

Solved

Get all Texts form Flexible Contents and nested Repeater

  • How can I get get all Texts form Flexible Contents and nested Repeater of a post in an sting for as post summary?

    I have the function below, that works find for all ACF Fields but not for Flexible Content Fields. Some flexible fields have nested repeater fields.

    
    	// function to get the conntent of all custom fileds fron array to one string
      function get_fields_content($include_fields, $fields) {
    	 	
    	 	
    	 	// loop the fields and add all content to one string
    		if( $fields ) {
    			foreach( $fields as $field_name => $field ):
    					$fieldname = $field['name']; // get the cutom field name for the filter
    					$fieldvalue = $field['value']; // get the content of the field	
    					
    					// filter by cutomfield from array and if value has content	
    					if ((in_array($fieldname, $include_fields) && ($fieldvalue))) { 
    						// add too $all a spache and a line break behind each field and add the html-stripped content from the next field
    						if ( !is_array($fieldvalue)) {
    							$all = $all . strip_tags($fieldvalue) . ' '; 
    						}	
    					}
    								
    			endforeach;					
    				
    			$all_content =  $all;
    		}
    	         
        return $all_content;
      }
    
    $fields = get_field_objects(); 
    $include_fields = array(
    	'field_1',
    	'field_2',
    	'field_3',
    	'field_4',
    	'field_5',
    );
  • Hi @bluesky

    You can always check the structure of a flexible content like this:

    var_dump( get_field('flexible_content_field_name') );

    So for a flexible content, you can do it like this:

    foreach( $fieldvalue as $row ){
        
        foreach( $row as $k => $column){
            
            if( $k !== 'acf_fc_layout' && !is_array($column) && !is_object($column) ){
                $all .= strip_tags($column) . ' ';
            }
            
        }
        
    }

    I hope this helps 🙂

  • Thanks!

    But it is actually not perfect, because this includes all sub-content-fields content an not only from field in the include_fields array…

    how can I add an
    in_array($fieldname, $include_fields)
    in your code?

  • Hi @bluesky

    In this case, you need to get the current field name from the column key like this:

    foreach( $fieldvalue as $row ){
        
        foreach( $row as $k => $column){
            
            if( $k !== 'acf_fc_layout' && !is_array($column) && !is_object($column) ){
    
                // get the current field name
                $current_field_name = str_replace($row['acf_fc_layout'].'_', '', $k);
    
                // check if in array
                if( in_array($current_field_name, $include_fields) ){
                    $all .= strip_tags($column) . ' ';
                }
            }
            
        }
        
    }

    I hope this helps 🙂

  • Perfect, thanx!

    this is mit current “final” function:

    	// function to get the conntent of all custom fileds fron array to one string ---------------------------------------------------------------------------------------
      function get_fields_content($include_fields, $fields) {
    	 	
    	 	
    	 	// loop the fields and add all content to one string
    		if( $fields ) {
    
    			foreach( $fields as $field_name => $field ):
    					$fieldname = $field['name']; // get the cutom field name for the filter
    					$fieldvalue = $field['value']; // get the content of the field	
    					
    					// filter by cutomfield from array and if value has content	
    					if ((in_array($fieldname, $include_fields) && ($fieldvalue))) { 
    						// add too $content a spache and a line break behind each field and add the html-stripped content from the next field
    						if ( !is_array($fieldvalue)) {
    							$content .=  strip_tags($fieldvalue) . ' '; 
    						}	else {
    						
    							// for the subfields if acf flexible content layouts ----------------------------
    							// https://support.advancedcustomfields.com/forums/topic/get-all-texts-form-flexible-contents-and-nested-repeater/
    							foreach( $fieldvalue as $row ){
    	    
    									foreach( $row as $k => $column){
    	        
    										if( $k !== 'acf_fc_layout' && !is_array($column) && !is_object($column) ){
    	
    											// get the current field name
    											$current_field_name = str_replace($row['acf_fc_layout'].'_', '', $k);
    	
    												// check if in array
    												if( in_array($current_field_name, $include_fields) ){
    													$content .= strip_tags($column) . ' ';
    	            							}
    											}
    	        
    			  							}
    									}
    								// end subfields ----------------------
    						}
    					}
    								
    			endforeach;					
    		}
    	         
        return $content;
      }
    
      // -------------------------------------------------------------------------
    
    

    and to get the fields and call the function:

    	
    $fields = get_field_objects(); 
    			$include_fields_content = array('acf_field_1' , 'acf_field_2' , 'acf_field_3);
    			$content = get_fields_content($include_fields_content, $fields);	
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Get all Texts form Flexible Contents and nested Repeater’ is closed to new replies.