0

I'm trying to display on my WORK page a value of a ACF field that lives inside a block in a custom post type project.

If I get all posts using Timber function like this:

$posts = Timber::get_posts('post_type=work');

I get the result:

array (size=1)
  0 => 
    object(Timber\Post)[1669]
      public 'ImageClass' => string 'Timber\Image' (length=12)
      public 'PostClass' => string 'Timber\Post' (length=11)
      public 'TermClass' => string 'Timber\Term' (length=11)
      public 'object_type' => string 'post' (length=4)
      public 'custom' => 
        array (size=3)
          '_edit_lock' => string '1565259680:1' (length=12)
          '_thumbnail_id' => string '30' (length=2)
          '_wp_old_slug' => string 'harrods-interior' (length=16)
      protected '_content' => null
      protected '_permalink' => null
      protected '_next' => 
        array (size=0)
          empty
      protected '_prev' => 
        array (size=0)
          empty
      protected '_css_class' => null
      public 'id' => int 29
      public 'ID' => int 29
      public 'post_author' => string '1' (length=1)
      public 'post_content' => string '<!-- wp:acf/hero-case-study {
    "id": "block_5d4ac446baf60",
    "name": "acf\/hero-case-study",
    "data": {
        "hero_cs_headline": "Lorem ipsum dolor sit amet consectetur adipisicing elit.",
        "_hero_cs_headline": "field_5d4aa12b9cf8b",
        "headline_cs_text_color": "#ffffff",
        "_headline_cs_text_color": "field_5d4ab40954b40",
        "category_cs_text_color": "#3c304c",
        "_category_cs_text_color": "field_5d4ab5205ffa8",
        "hero_cs_bg_image": 42,
        "_hero_cs_bg_'... (length=6122)
      public 'post_date' => string '2019-08-06 16:41:36' (length=19)
      public 'post_excerpt' => string '' (length=0)
      public 'post_parent' => int 0
      public 'post_status' => string 'publish' (length=7)
      public 'post_title' => string 'KTURE' (length=5)
      public 'post_type' => string 'work' (length=4)
      public 'slug' => string 'kture' (length=5)
      protected '__type' => null
      public '_edit_lock' => string '1565259680:1' (length=12)
      public '_thumbnail_id' => string '30' (length=2)
      public '_wp_old_slug' => string 'harrods-interior' (length=16)
      public 'post_date_gmt' => string '2019-08-06 16:41:36' (length=19)
      public 'comment_status' => string 'closed' (length=6)
      public 'ping_status' => string 'closed' (length=6)
      public 'post_password' => string '' (length=0)
      public 'post_name' => string 'kture' (length=5)
      public 'to_ping' => string '' (length=0)
      public 'pinged' => string '' (length=0)
      public 'post_modified' => string '2019-08-08 08:06:38' (length=19)
      public 'post_modified_gmt' => string '2019-08-08 08:06:38' (length=19)
      public 'post_content_filtered' => string '' (length=0)
      public 'guid' => string 'http://simplicitypartners2019.test/?post_type=work&#038;p=29' (length=60)
      public 'menu_order' => int 0
      public 'post_mime_type' => string '' (length=0)
      public 'comment_count' => string '0' (length=1)
      public 'filter' => string 'raw' (length=3)
      public 'status' => string 'publish' (length=7)

As you can see, what I need is the value of "hero_cs_headline" that is inside post_content. Can anyone help me top take this value?

If there is another solution right inside twig file, whould be great as well.

Thanks!

5
  • not sure about timber, but you can always get a certain field by its post id, for example get_field('field_name', 29); where 29 is the number i fetched from your code example..?
    – honk31
    Aug 8, 2019 at 14:37
  • Thanks but it doesn't help much. get_field() doesn't work as it's inside a block. Aug 9, 2019 at 10:28
  • not really sure, what you mean by that. you are aware of parse_blocks..?
    – honk31
    Aug 12, 2019 at 8:51
  • Yes. I'm using parse_blocks to get all block content and then I'm getting my ACF field from there. But what I'm looking for here is an easiest way to get fields values that are stored inside a block, but I need to get it from another page or template. From my understanding there's no way or native function to get those fields. Thanks by the way! Aug 12, 2019 at 10:30
  • you need to share your code, i can't really follow. might have to do with timber/twig, i'm not into that, but lets see..
    – honk31
    Aug 12, 2019 at 15:07

1 Answer 1

0

You'll need to set a render callback when you create your ACF block:

acf_register_block_type( array(
    'name'            => 'example-block',
    'title'           => __( 'Example Block', 'your-text-domain' ),
    'description'     => __( 'A custom example block.', 'your-text-domain' ),
    'render_callback' => 'my_acf_block_render_callback',
    'category'        => 'formatting',
    'icon'            => 'admin-comments',
    'keywords'        => array( 'example' ),
) );

Then in that function you call Timber functions like in a page template:

function my_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
    $context = Timber::context();

    // Store block values.
    $context['block'] = $block;

    // Store field values.
    $context['fields'] = get_fields();

    // Store $is_preview value.
    $context['is_preview'] = $is_preview;

    // Render the block.
    Timber::render( 'block/example-block.twig', $context );
}

After that the block will properly render when output using {{ post.content }} or anything else that uses the standard Wordpress filters to display the_content().

More info is available in Timber's documentation, which is where I got these snippets.

1
  • Additional tip: You can reuse the same function if you use the block's name in the twig file's name, like Timber::render( 'block/' . $block['name'] . '.twig', $context );, though you'll have to remove the acf/ that is automatically prefixed to it.
    – cbirdsong
    Feb 13, 2020 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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