Support

Account

Home Forums ACF PRO ACF PRO: Label + Value pairs of Field groups in front end – almost there, but…

Solved

ACF PRO: Label + Value pairs of Field groups in front end – almost there, but…

  • Hallo,

    I’m doing a wine website and trying to echo out all custom fields on given posts.

    The code below is working fine (but…):

    <?php 
    
    $fields = get_field_objects();
    
    if( $fields )
    {
    	foreach( $fields as $field )
    	{
    		$value = get_field( $field['name'] );
    		
    		echo '<dl>';
    			echo '<dt>' . $field['label'] . '</dt>';
    			echo '<dd>' .$value . '</dd>';
    		echo '</dl>';
    	}
    }
    
     ?>

    Yet, as the custom fields used in the post are items of two separate field groups (‘organolepsy’ and ‘techspecs’), I’m stuck on trying to echo out fields in each group (let’s say to have them in two separate tabs).

    I’ve had some luck in echoing out the two separate field grroups as array items, but they seemingly fail to load the respective values.

    Feels I’m almost there, but :'(

    Any help would be much appreciated!

    Thanks for your help, and kudos for Eliot’s great work for the community.

  • Hi @alessietto

    You can get the field group list by using the acf_get_field_groups() function. Please check the answer in this thread to learn more about it: https://support.advancedcustomfields.com/forums/topic/field-always-returns-null/.

    I hope this helps 🙂

  • Hi @james,

    thank you! you definitely put me on the right track here.

    I have achieved the desired result, and also managed to unecho unwanted groups here.

    I’ll mark this as solved, post my code below in case it might help others, and kindly ask you to review my code in case you think there could be smarter ways to get to the same point:

    <?php
    
    $groups = acf_get_field_groups(array('post_id' => get_the_ID()));
    foreach( $groups as $group_key => $group ) {
        $fields = acf_get_fields($group);
    
    // below starts condition to unecho unwanted field groups 
    	 if($group['title'] !== 'pagelicks'){
    	    echo '<h2>'.$group['title'].'</h2>';
    		echo '<dl class="techspecs">';
    	    if($fields ) {
    	        foreach( $fields as $field_name => $field ){
    		        $value = get_field($field['name']);
    	        	if( $value && $field['label'] !=='Bollicine' && $field['label'] !=='tipologia prodotto' ) {
    		            echo '<dt>' . $field['label'] . '</dt>';
    		            echo '<dd>' . $value. '</dd>';
    	            }
    	        }
    	    echo '<dl>';
    	    }
    	// below ends condition to unecho unwanted field groups
    	}
    }
    ?>

    Your help was priceless, thanks again!

    Alessio

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

You must be logged in to reply to this topic.