Is it possible to exclude a block if a field is selected? I'm using get_field('article_style' ) == 'major'
and if it's selected to not have the block acf/opby-cover-image
show up on my theme. I'm using an add_filter
from Skip Certain Gutenberg Block in Loop.
The code I have below does work if the field is selected but if it's not then it doesn't display the rest of the content. Any ideas on how to not show this block if the field is selected but show the content if the field is not selected?
function remove_blocks() {
if ( is_single() && (get_field('article_style' ) == 'major') ) {
$blocks = parse_blocks( get_the_content() );
foreach ( $blocks as $block ) {
if ( 'acf/opby-cover-image' === $block['blockName'] ) {
continue;
} else {
echo render_block( $block );
}
}
}
}
add_filter( 'the_content', 'remove_blocks');
Do the conditional check maybe in the loop instead of before?
if ( is_single() ) {
$blocks = parse_blocks( get_the_content() );
foreach ( $blocks as $block ) {
if ( 'acf/opby-cover-image' === $block['blockName'] && (get_field('article_style' ) == 'major') {
continue;
} else {
echo render_block( $block );
}
}
}