Support

Account

Home Forums Gutenberg ACF 5.8 – Parse Gutenberg blocks and get ACF data outside of post Reply To: ACF 5.8 – Parse Gutenberg blocks and get ACF data outside of post

  • I also ran into this question and solved it with some modifications to the answer given earlier. Maybe it will help someone.

    function fetch_large_slider($postId) {
    	$slider_images = [];
    	$pid = (empty($postId) ? get_post() : $postId);
    
    	// check if acf is used
    	if (function_exists('get_field')) {
    		if (has_blocks($pid)) {
    			$blocks = parse_blocks( $pid->post_content );
    
    			// run trough acf gutenberg/acf-blocks and find the right one
    			foreach ( $blocks as $block ) {
    				// find all selected images
    				if ($block["blockName"] == 'acf/full-page-slider') {
    					// build new array from found images
    					$image_set = $block["attrs"]["data"]["fullpage_slider_images"];
    					// collect image urls inside an array
    					foreach ($image_set as $imageID) {
    						$slider_images[] = wp_get_attachment_image_src($imageID,'full',false)[0];
    					}
    				} else {
    					continue;
    				}
    			}
    		}
    	}
    	return $slider_images;
    }