Shoora
4/22/2019 - 10:22 AM

ACF template output

ACF template output

<?php
/**
 * Template Name: ACF DEV
 * Template Post Type: park
 * @package adp
 */
?>

<pre>
<code>
<?php 


function generate_acf_output($type, $name, $isSub ) {
    $fieldsBasic = [
        'text',
        'wysiwyg',
        'textarea',
    ];

    $fieldsWithChildren = [
        'repeater',
    ];    

    $output = [];
    $output[1] = '';
    $get = 'get_field';
    $the = 'the_field';
    $subLbl = '';
    if ($isSub) {
        $get = 'get_sub_field';
        $the = 'the_sub_field';
        $subLbl = ' subfield';
    }

    // image
    if ($type == 'image') {
        $output[0] =
'<?php $attachment_id = ' .$get. '("'. $name .'");
$imgObj = wp_get_attachment_image_src( $attachment_id["id"], "full" );
$imgUrl = $imgObj["0"]; ?>'
;
}
    // posts object
    elseif ($type == 'post_object') {
        $output[0] =
'<?php $post_object = ' .$get. '("'. $name .'");
if( $post_object ): 
// override $post
$post = $post_object;
setup_postdata( $post ); 
?>
<!-- post content here -->';
     $output[1] = 
'
<!-- post content end -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>';
    }

    // fields with children
    elseif (in_array($type, $fieldsWithChildren)) {
       $output[0] =
'<?php if(' .$get.'("'. $name .'")): ?>
<?php while(has_sub_field("'. $name .'")): ?>
<!-- sub_fields_here -->'. "\n";
        $output[1] .=
'<?php while; ?>
<?php endif; ?>';

    }
    // fields with children
    elseif ($type == 'flexible_content') {
       $output[0] =
'<?php if(' .$get.'("'. $name .'")): ?>
<?php while(has_sub_field("'. $name .'")): ?>
<!-- sub_fields_here -->'. "\n";
        $output[1] .=
'<?php while; ?>
<?php endif; ?>';

    }


    //Basic Fields Markup
    elseif (in_array($type, $fieldsBasic)) {
       $output[0] = 
'<?php if(' .$get.'("'. $name .'")): ?>
<?php ' .$the.'("'. $name .'"); ?>
<?php endif; ?>';
} else {
    $output[0] = 'Got nothing for this field type hit the docs! TYPE:  ' . $type . '  NAME:  ' . $name;
}
    $output[1] .= "\n". "<!--  *** end ".$type.$subLbl." *** -->\n\n";
    return $output;
}

function output_acf_template_code() {
$fields = get_field_objects();
if( $fields ) {
    foreach( $fields as $field ){
    echo '<div>';
        $l1 = generate_acf_output($field['type'], $field['name'], false);
        echo htmlspecialchars($l1[0]);            
        if (array_key_exists('sub_fields', $field)) {
       
            foreach( $field['sub_fields'] as $sfield ) {
                $l2 = generate_acf_output($sfield['type'], $sfield['name'], true);
                echo htmlspecialchars($l2[0]);
               
                if (array_key_exists('sub_fields', $sfield)) {

                    foreach( $sfield['sub_fields'] as $ssfield ) {
                    $l3 = generate_acf_output($ssfield['type'], $ssfield['name'], true);
                    echo htmlspecialchars($l3[0]);                            
                        if (array_key_exists('sub_fields', $ssfield)) {

                            foreach( $ssfield['sub_fields'] as $sssfield ) {
                                $l4 = generate_acf_output($sssfield['type'], $sssfield['name'], true);
                                echo '<div> **** 4th level subfield... additional children of this nested subfield will not be outputted, check ACF\n';
                                echo htmlspecialchars($l4[0]);                                 
                                echo htmlspecialchars($l4[1]);                                 
                                echo '</div>';
                            }
                        }
                    echo htmlspecialchars($l3[1]);     
                    }
                }
            echo htmlspecialchars($l2[1]);     
            }
        }
        echo htmlspecialchars($l1[1]);
    echo '</div>';
    }
} 
}

output_acf_template_code();

?>
</code>
</pre>