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
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
Share Improve this question edited Aug 20, 2020 at 13:48 Stefan Avramovic asked Aug 20, 2020 at 11:10 Stefan AvramovicStefan Avramovic 1013 bronze badges 1- Does the answer solved your problem. Do let me know. – Behemoth Commented Aug 20, 2020 at 14:00
1 Answer
Reset to default 0You 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' ) {
}
}