最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Populate acf-field, depending on block name

programmeradmin3浏览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

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
Add a comment  | 

1 Answer 1

Reset to default 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' ) {
  
    }
}
发布评论

评论列表(0)

  1. 暂无评论