Support

Account

Home Forums Gutenberg ACF context for secondary loops in Gutenberg blocks

Unread

ACF context for secondary loops in Gutenberg blocks

  • I have a Gutenberg block that includes a secondary loop, and in that loop I need to get ACF values. I can do this by specifying a post ID as the second parameter of the_field(). However, this won’t work in my situation. I need to get ACF values without specifying that second parameter. Is there a way to do this in a Gutenberg block? Can I update the ACF context somehow?

    Here’s an example:

    
    $args = array(
      'post_type' => 'cpt_slug',
    	'p' => $cpt_id,
    );
    $the_query = new WP_Query( $args );
    if ($the_query->have_posts()) {
      while ($the_query->have_posts()) {
        $the_query->the_post();
    
        the_field('acf_slug'); // This works in a page template, but not in a Gutenberg block template.
        the_field('acf_slug', get_the_ID()); // This does work in a Gutenberg block template, but I can't use this for reasons.
    
      }
    }
    wp_reset_postdata();
    

    If needed I can explain why I can’t use the second parameter.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.