Support

Account

Home Forums Gutenberg Displaying Gutenberg block from archive page

Solved

Displaying Gutenberg block from archive page

  • Hi there.

    I was wondering if it’s possible to grab an acf gutenberg block field located in a single post and display it on an archive page?

    Do I need to somehow retrieve the block id and/or post id?

    Thanks

  • If anyone is interested I found a solution to this in another thread – https://support.advancedcustomfields.com/forums/topic/acf-5-8-parse-gutenberg-blocks-and-get-acf-data-outside-of-post/

    <?php if (have_posts()) :
      while (have_posts()) : the_post();?>
        <?php if ( function_exists( 'get_field' ) ) {
          $pid = get_post();
          if ( has_blocks( $pid ) ) {
            $blocks = parse_blocks( $pid->post_content );
            foreach ( $blocks as $block ) {
              if ( $block['blockName'] === 'acf/your-acf-block' ) {
                $field = $block['attrs']['data']['your_acf_field'];
              } 
            }
          }
        } ?>
        <?php echo $field; ?>
      <?php endwhile; ?>
    <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.