Support

Account

Home Forums ACF PRO get_field_object within loop

Unread

get_field_object within loop

  • Setup: I have two templates and use the Genesis Framework hooks to populate them:

    • event-template.php: displays all items of CPT ‘event’ (using a loop)
    • single-event-template.php: displays a single event

    The event-page uses ACF fields, among which a select field.
    When calling get_field_object on the single-event-template the values display correctly. When I call get_field_object on the event-template (i.e. within the loop), no values are returned.
    Note that get_field correctly returns the value of another field in both situations.

    Code on both pages:

    $eventTypeObject    = get_field_object( 'event_type' );
    	$eventTypeValueArr  = $eventTypeObject['value'];
    	$eventTypeValue     = $eventTypeValueArr[ 'value' ];
    	$eventTypeLabel     = $eventTypeValueArr[ 'label' ];
    	$custom_event_type  = get_field( 'custom_event_type' );
    
    	?>
        <ul>
            <li>get_field_object: <?php echo print_r($eventTypeObject) ?></li>
            <li>field_object['value']: <?php echo print_r($eventTypeValueArr) ?></li>
            <li>field_object['value']['value']: "<?php echo $eventTypeValue ?>"</li>
            <li>field_object['value']['label']: "<?php echo $eventTypeLabel ?>"</li>
            <li>get_field('custom_event_type'): "<?php echo $custom_event_type ?>"</li>
        </ul>
    
    	<?php

    Output on single-event.php:

    get_field_object: Array ( [ID] => 130 [key] => field_5b2a622f49bdc [label] => Soort event [name] => event_type [prefix] => acf [type] => select [value] => Array ( [value] => workshop [label] => Workshop ) [menu_order] => 4 [instructions] => [required] => 0 [id] => [class] => [conditional_logic] => 0 [parent] => 82 [wrapper] => Array ( [width] => [class] => [id] => ) [_name] => event_type [_prepare] => 0 [_valid] => 1 [choices] => Array ( [course] => Cursus [lecture] => Lezing [masterclass] => Masterclass [workshop] => Workshop [custom] => Anders ) [default_value] => Array ( ) [allow_null] => 0 [multiple] => 0 [ui] => 0 [ajax] => 0 [return_format] => array [placeholder] => ) 1
    field_object['value']: Array ( [value] => workshop [label] => Workshop ) 1
    field_object['value']['value']: "workshop"
    field_object['value']['label']: "Workshop"
    get_field('custom_event_type'): "Dit is een meesterklas"
    
    field_object['value']: Array ( [value] => workshop [label] => Workshop ) 1
    field_object['value']['value']: "workshop"
    field_object['value']['label']: "Workshop"
    get_field('custom_event_type'): "Dit is een meesterklas"
     

    Output on event-template.php:

    get_field_object: 1
    field_object['value']: 1
    field_object['value']['value']: ""
    field_object['value']['label']: ""
    get_field('custom_event_type'): "Dit is een meesterklas"

    Question: why are the values not returned on the event-template.php page?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.