Support

Account

Home Forums General Issues Shortcode to print all fields of a group

Unread

Shortcode to print all fields of a group

  • Hi there. I have some difficulties with a shortcode, the goal is to print the values and labels of all fields from a group. Currently I have this code:

    
    function group_shortcode($atts){
        $group = get_field($atts[0]);
    	echo '<div class="">';
        foreach($group as $key => $value) {
    		echo '<div class="acf-label">' . $key . '</div>';
    		echo '<div class="acf-value">' . $value. '</div>';
        }
    	echo '</div>'; 
    }
    add_shortcode('acf_group', 'group_shortcode');
    

    The problem is, this prints the name of each field instead of the label. I tried to achieve this with get_field_objects, but I got stuck there. My custom field is structured like this:

    Custom Product:
    -field 1
    -field 2
    -field 3
    –sub 1
    –sub 2
    –…
    -field 4

    As I said, i want to print all sub fields from field 3. I had a similar case on another website where I put the code directly into the single product page template. I also tried to convert that code into a shortcode without any luck.

    $groups = acf_get_field_groups(array('post_id' => get_the_ID()));
    	foreach( $groups as $group_key => $group ) {
    		$fields = acf_get_fields($group);
    
    		 if($group['title'] == 'vehicledata'){
    			echo '<h2>'.$group['title'].'</h2>';
    			echo '<div class="">';
    			if($fields ) {
    				foreach( $fields as $field_name => $field ){
    					$value = get_field($field['name']);
    						echo '<div class="car-label">' . $field['label'] . '</div>';
    						echo '<div class="car-value">' . $value. '</div>';
    				}
    			echo '</div>';
    			}
    		}
    	}

    I would really appreciate some help. Thanks

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.