Using get_extended with blocks
Published: – Leave a comment
Since long time, WordPress has the possibility to define the amount of content displayed on the posts list archive page by adding a “more” tag to the content. While using the content until this tag is easily possible using get_the_content
, what about using the rest?
More specific, what about using the content after the “more” tag? That’s where the function get_extended
comes in place. This function, however, does not only return the content after the “more” tag, but rather returns an array with three parts of the content:
main
– The content before the “more” tag (i.e. the excerpt when usingget_the_content
).extended
– The content after the “more” tag.more_text
– The custom “more” text, if any, otherwise an empty string.
So if you want to get the want to get the text after the “more” tag, you want to use get_extended( $post->post_content )['extended']
.
However, for posts with the block editor, you’re still not at the end here.
First the extended
content also contains the “more” block itself (<!-- /wp:more -->
).
Second, the content is not properly prepared to be directly usable to output it. You need to run a do_blocks
on it.
So this is what it actually looks like getting only the part after the “more” tag from a post written on the block editor (I also added the recommended escaping via wp_kses_post
):
echo wp_kses_post( do_blocks( str_replace( '<!-- /wp:more -->', '', get_extended( $post->post_content )['extended'] ) ) );
Code language: PHP (php)
One last note here: If there is no “more” tag available, the whole post content is part of the array key main
and the key extended
is empty.