Ok so after some digging I’m not sure if this is an ACF or Gutenberg issue. I decided to try and check if a core/heading or core/paragraph have any content in them. My code is more for debugging at the moment.
I have used pretty much the same code as the OP in my block markup.
global $post;
// Get all blocks on the page.
$blocks = parse_blocks( $post->post_content );
// Loop over the blocks.
foreach ( $blocks as $block ) {
// Check for the accordion block.
if ( $block['blockName'] === 'acf/accordion' ) {
// Check if any inner blocks are used. There is an empty core heading and core/paragraph block.
if ( $block['innerBlocks'] ) {
$block_innerBlocks = $block['innerBlocks'];
foreach ( $block_innerBlocks as $inner_block ) {
// Check for the core heading block.
if ( $inner_block['blockName'] === 'core/heading' ) {
// This is where things get messy! We need to check if the core block has any content but its outputting the html element markup even if nothing has been entered.
var_dump($inner_block['innerHTML']);
var_dump($inner_block['innerContent']);
}
}
}
}
}
So if you check the var_dump you will notice that even if a core block has no content entered it will still output the actual <h2…. markup as a string.
Dumping just $inner_block I can’t see that there anything that can actually be used to conditionally check if content is in there or not?
I could go down the route of checking the string length of $inner_block[‘innerHTML’] or $inner_block[‘innerContent’] but thats a flawed idea as class names could change and the string length value will change.
Anybody got any suggestions?
Just wanted to bump this, as I have the same issue. Empty core blocks still generate their html markup on the front end even if the core fields are empty and there does not seem any way to check for this?
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.