• kmytort

    (@kmytort)


    Hello, I’m using this function to call a block
    but what I want is to use the second block of media

    
    
     $blocks = parse_blocks($post->post_content);
        foreach ($blocks as $block) {
            if ('core/media-text' === $block['blockName']) {
                echo apply_filters('the_content', render_block($block));
                break;
            }
        }

    if i use continue; it loads all the media-text blocks and i just want to use the second one

    I made this code but I can’t understand how it works, can you help me ?

         foreach ($blocks as $block) {
         if ($blocks[0]['blockName'] === 'media-text') {
         if ($block['blockName'] === 'media-text' && $block_counter === 0) {
         $block_counter++;
         continue;
         }
         }
         }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Instead of combining two conditions in one if() statement, break it out into two nested if()s. The inner if() is to capture the actual block you are after. The outer if() is just to ensure the counter is only incremented when a media-text block is encountered. Assuming that $block_counter was initialized to 0 prior to entry, the second media-text block you’re after will occur when 1 == $block_counter. When that happens, you can echo it out or do what ever you wish to do with it. Be sure the $block_counter increment is in the outer if(). You don’t really need continue; since nothing will happen except when 1 == $block_counter.

    You can let the foreach() loop run to its natural conclusion. For a very large amount of content to parse, it might be beneficial to break out of the loop once the correct block is found and output. It’s not absolutely necessary, but the loop may run more efficiently.

    Thread Starter kmytort

    (@kmytort)

    Hello,
    if (outer ) {//if-outer
    if (inner) { //if inner
    # code…
    }
    }

    thanks for your help
    when you mean indoor and outdoor is it in this structured way?

    Moderator bcworkz

    (@bcworkz)

    That’s right, one conditional nested within the other. Something like

    $block_counter = 0;
    if ( $block['blockName'] === 'media-text' {
         if ( $block_counter === 1 ) {
               // do something with the second found media block
         }
         $block_counter++;
    }
    Thread Starter kmytort

    (@kmytort)

    hello,
    I think I did it wrong, I still need to learn more logic hehehe
    , The strange thing is that it works but it does not take the value of the media/text, it takes everything in general and I had to search the array one by one to find it.

    $block_count = 0;
    
    	foreach ($blocks as $block) {
    		$block_count++;
    
    		if ($block_count == 1) {
    			if ($block['blockName'] === 'core/media-text') {
    				echo render_block($block);
    				//echo ('epa col' . '/' . $block_count);
    				//var_dump($block);
    			}
    		}
    
    	}
    Moderator bcworkz

    (@bcworkz)

    There may be another level of data you need to parse through to get what you need. It may mean yet another nested conditional, or reaching farther into an array some where along the way. You should probably dump out the data at key steps so you can see what you’re actually working with so you can best proceed to the next step in the most optimal manner.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘load separate block from THE_conten()’ is closed to new replies.