Support

Account

Home Forums ACF PRO Loop on location fields : dynamically creation

Solved

Loop on location fields : dynamically creation

  • Hi,

    Is it possible to dynamically set location for custom fields ?

    Instead of :

        'location' => array (
            array (
                array (
                    'param' => 'page',
                    'operator' => '==',
                    'value' => '1696',
                ),
            ),
            array (
                array (
                    'param' => 'page',
                    'operator' => '==',
                    'value' => '1762',
                ),
            ),
        ),

    something like :

    foreach($ids as $id){
        'location' => array (
            array (
                array (
                    'param' => 'page',
                    'operator' => '==',
                    'value' => $id,
                ),
            ),
    }

    I can’t find how to write it inside PHP field generated code. I want to do that cause I can get ids of my translated pages (Polylang) so I want to affect dynamically my customs fields to these pages.

    Maybe can I reaffect location after…? (how…?)

    Thanks for any help.

  • Hi @manhattanjazz

    I believe you can do it like this:

    if( function_exists('acf_add_local_field_group') ):
        $ids = array('66','691');
        $args = array (
            ...
            ...
            ...
            'location' => array (
                array (
                    array (
                        'param' => 'page',
                        'operator' => '==',
                        'value' => '815',
                    ),
                ),
            ),
            ...
            ...
            ...
        );
    
        foreach( $ids as $id ){
            $args['location'][] = array (
                array (
                'param' => 'page',
                'operator' => '==',
                'value' => $id,
                ),
            );
        }
    
        acf_add_local_field_group($args);
    
    endif;

    I hope this helps.

  • Thanks a lot.
    I’ll come later to say if it allows me to perfectly use Polylang with ACF.

  • So, last but not least :
    It works ^^
    I can now dynamically assign my ACF custom fields in each Polylang language, thanks to you.

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

The topic ‘Loop on location fields : dynamically creation’ is closed to new replies.