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.
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?
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++;
}
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);
}
}
}
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.