Support

Account

Home Forums Gutenberg "Undefined index: id" when using the parse_blocks() function

Solved

"Undefined index: id" when using the parse_blocks() function

  • Hello,

    to get a field value from a custom acf block as teaser text i use the followoing parse_blocks functions in a query:

    
    $blocks = parse_blocks( get_the_content() );
    			foreach ($blocks as $block) {
    				if( 'acf/lead-in' == $block['blockName'] ) {
    
    					if( !empty( $block['attrs']['data']['lead_in_editor'] ) ) {
    						$title = $block['attrs']['data']['lead_in_editor'];
    						echo '<p>'. $title .'</p>';
    					}
    					
    				}
    			}

    Everything works as expected but i get the following error:

    
    Notice: Undefined index: id in E:\dir\dir\dir\dir\wp-content\plugins\advanced-custom-fields-pro\pro\blocks.php on line 423
    

    I tried to use it outside a query but with the same result.

    What i’m doing wrong? Many thanks in advance!

    —–
    Advanced Custom Fields PRO V. 5.11.4
    WordPress 5.8.2

  • Hello again,

    changing the name of the variable $block to something else in the foreach head fixed the error message.

    Example:

    
    			$blocks = parse_blocks( get_the_content() );
    			foreach ($blocks as $blockcontent) {
    				if( 'acf/blockname' == $blockcontent['blockName'] ) {
    					
    					
    					if( !empty( $blockcontent['attrs']['data']['field_name'] ) ) {
    						$title = $blockcontent['attrs']['data']['field_name'];
    						echo '<p>'. $title .'</p>';
    						break;
    					}
    					
    				}
    			}
    

    Maybe someone can explain me that? Thanks again.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.