Populate acf-field, depending on block name

Question

I have an acf select “global_fargtema” for all blocks

what i am trying to do is, if a block name is == “acf/full-width-talker”
populate the field “global_fargtema” with $object->ID = “1”; $object->post_title =”Full width talker”;
if a block name is == “acf/usp” $object->ID = “1”; $object->post_title =”USP”;

There cane be multiple blocks on one page, any idea on how this can be achived?

   function acf_load_theme_field_choices($field)
    {
        $current_blog_id = get_current_blog_id();
    
        // reset choices
        $field['choices'] = array();
    
        if (is_multisite()) {
            switch_to_blog(1);
        }
    
        
        $object = new stdClass();
    
        
                $object->ID = "1";
                $object->post_title ="Full width talker";
     
    
    
        $enhetsplats = array($object);
    
        if (is_multisite()) {
            restore_current_blog();
        }
    
        // loop through array and add to field 'choices'
        if (is_array($enhetsplats)) {
            foreach ($enhetsplats as $enhet) {
                $field['choices'][$enhet->ID] = $enhet->post_title;
            }
        }
    
        return $field;
    }
    
    add_filter('acf/load_field/name=global_fargtema', 'acf_load_theme_field_choices');

I have tried this:

$post = get_post();
if ( has_blocks( $post->post_content ) ) {
    $blocks = parse_blocks( $post->post_content );
    if ( $blocks[0]['blockName'] === 'acf/full-width-talker' ) {
  
    }
}

but $post seams to return empty..

0
, Stefan Avramovic 3 years 2020-08-20T07:10:27-05:00 0 Answers 36 views 0

Leave an answer

Browse
Browse