Support

Account

Home Forums Front-end Issues HowTo: show ALL acf fields and their labels

Solved

HowTo: show ALL acf fields and their labels

  • I’ve noticed that there is no function to retrieve all custom fields from a post which are generated by acf. And because acf has different types of fields, i needed a more complex function to get all fields and once without calling each field with get_field and the_field. so this is what i wrote. simply insert this themes template file where you need it (i.e. content.php):

    <?php
    $i = 0;
    $fields = get_fields($post_id);
    // unset some fields you don't want to output:
    unset($fields["bild"]);
    unset($fields["asin"]);
    unset($fields["preis"]);
    unset($fields["bewertung"]);
    unset($fields["produktname"]);
    echo "<div class='data'>";
    foreach($fields as $fieldname => $fieldvalue){
    	if($fieldvalue == "") continue;
    	echo "<div class='item'>";
    		$fieldobj = get_field_object($fieldname, false, array('load_value' => false));
    		echo "<div class='term'>".$fieldobj["label"]."</div>";
    		if(is_array($fieldvalue)){
    			foreach($fieldvalue as $arrayvalue){
    				$newarray[] = $fieldobj['choices'][ $arrayvalue ];
    			}
    			$value = implode(", ",$newarray);
    			
    		} elseif (isset($fieldobj['choices'][ $fieldvalue ])) {
    			$value = $fieldobj['choices'][ $fieldvalue ];
    		} else {
    			$value = $fieldvalue;
    			if($append = $fieldobj["append"]) $value .= " ".$append;
    		}
    		echo "<div class='value'>".$value."</div>";
    	echo "</div>";
    	$i++;
    }
    echo "</div>";
    ?>

    this will generate a list like the one in the attachment.

  • Hi @nicmare,

    Actually there is such a function ๐Ÿ™‚
    get_field_objects()

    http://www.advancedcustomfields.com/resources/get_field_objects/

  • damn it ^^. then one could use it that way:

    <?php
    if($fields = get_field_objects()){
    unset($fields["bild"]);
    unset($fields["asin"]);
    unset($fields["preis"]);
    unset($fields["bewertung"]);
    unset($fields["produktname"]);
    ?>
    <div class='data row'>
    	<?php
    	foreach($fields as $field){
    		?>
    		<div class='item col-sm-6 col-md-4 col-lg-3'>
    			<div class='term'>
    				<?php echo $field["label"];?>
    			</div>
    			<div class="value">
    				<?php 
    				if(isset($field["prepend"])) echo $field["prepend"]." ";
    				if(isset($field["choices"])){
    					if(is_array($field["value"])){
    						$newarray = array();
    						foreach($field["value"] as $value){
    							$newarray[] = $field["choices"][$value];
    						}
    						echo implode(",",$newarray);
    					} else {
    						echo $field["choices"][$field["value"]];
    					}
    				} else { 
    					echo $field["value"]; 
    				} 
    				if(isset($field["append"])) echo " ".$field["append"];
    				?>
    				</div>
    		</div>
    		<?php
    	}
    	?>
    </div>
    <?php
    }
    ?>
    

    this will generate nice list with pre- and appended elements and will look for right labels for checkboxes/select fields

  • Yeah ๐Ÿ™‚

    It’s an easy function to miss as it looks so much alike the others. But I’m glad I could help! Be sure to really scan the available functions/actions/filters in the documentation before attempting something hacky, there’s actually quite a bit there!

  • dear jonathan,

    i’ve noticed that the function sorts the array by field id. but you have an idea how to sort them like they are arranged in plugin settings (drag an drop order)?

  • Am I right in thinking that get_field_objects() only returns fields that have a value?
    If so, is there a way to return all fields (empty or not) from the field group?

    I’m looking to create a progress indicator that displays how much of the form has been completed.

  • @nobby get_fields returns fields with values only.
    get_field_objects() – “Returns the settings of all fields saved on a specific post.”.
    For your purposes I suggest to use the definition in acf-json.
    The only issue is that there is no API, meaning that if the structure changes,
    your solution need to adapt to it.

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

You must be logged in to reply to this topic.