Home › Forums › Gutenberg › Conditional to check if an instance of a block is the first on page/post › Reply To: Conditional to check if an instance of a block is the first on page/post
Hey there, I would do this to get the last block.
In addition I added a param to get the post we want by ID and a check to avoid error such as “Trying to get property ‘post_content’ of non-object”
function friss_get_last_block_id($post=null) {
if(is_null($post)){
global $post;
}elseif(is_int($post)){
$post = get_post($post);
}
if($post instanceof WP_Post){
if(has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
$last_block = end($blocks);
$last_block_attrs = $last_block['attrs'];
if(isset($last_block_attrs['id'])) {
return $last_block_attrs['id'];
}
}
}
return false;
}
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!
Make sure to register for tomorrow’s special ACF-focused edition of #BuildModeLive. Iain Poulson joins @hellosammunoz & @bgardner for a discussion about ACF’s roadmap and what’s in development. https://t.co/k93h4pYCae
— Advanced Custom Fields (@wp_acf) January 26, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.