0

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)
    $field['choices'] = array(
        'custom'    => 'My Custom Choice',
        'custom_2'  => 'My Custom Choice 2'
    );
    return $field;
}
        

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' ) {
      
add_filter('acf/load_field/name=global_fargtema', 'acf_load_theme_field_choices');
        }
    }

but it did not work

1
  • Does the answer solved your problem. Do let me know.
    – Behemoth
    Aug 20 '20 at 14:00
0

You are calling get_post() using null. Try to retrieve the post individually. Use get_post(get_the_ID()) to get the post content.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.