Conditional to check if an instance of a block is the first on page/post (ok)
https://support.advancedcustomfields.com/forums/topic/conditional-to-check-if-an-instance-of-a-block-is-the-first-on-page-post/
/** * Get ID of the first ACF block on the page */ function sg_get_first_block_id() { $post = get_post(); if(has_blocks($post->post_content)) { $blocks = parse_blocks($post->post_content); $first_block_attrs = $blocks[0]['attrs']; if(array_key_exists('id', $first_block_attrs)) { return $first_block_attrs['id']; } } }<?php if($block['id'] === sg_get_first_block_id()): ?> <h1><?php the_title(); ?></h1> <?php endif; ?><?php if($block['id'] === sg_get_first_block_id()): ?> <h1><?php echo $title; ?></h1> <?php else: ?> <h2><?php echo $title; ?></h2> <?php endif; ?>/** * Check first block type * @param string block_handle - handle of the block */ function sg_first_block_is($block_handle) { $post = get_post(); if(has_blocks($post->post_content)) { $blocks = parse_blocks($post->post_content); if($blocks[0]['blockName'] === $block_handle) { return true; }else { return false; } } }if(!sg_first_block_is('acf/sg-cover')): get_template_part('template-parts/components/component', 'breadcrumbs'); echo '<h1>' . get_the_title() . '</h1>'; endif;
PreviousWordpress check if blog post (ok)NextFull list of wp globals, List of global WP variables (ok)
Last updated