• Hello guys,

    I developed custom blocks for my news blog posts and the_excerpt is not working anymore.

    <!-- wp:qsd/section -->
    <!-- wp:qsd/container {"className":"container"} -->
    <!-- wp:paragraph -->
    <p>Hello World.</p>
    <!-- /wp:paragraph -->
    <!-- /wp:qsd/container -->
    <!-- /wp:qsd/section -->

    That’s my structure of every blog post. If i create a simple paragraph before the custom block the_excerpt is working for that paragraph.

    I tried stuff with get_the_content(), serialize_blocks(), parse_blocks() and str_replace(). But this solution is so messy!! There must be another way, right?

    Thanks for help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    the_excerpt() works by taking content from the raw content data. It then strips out all HTML tags so it can accurately count the words. If you have blocks that dynamically insert content, the_excerpt() cannot “see” that content.

    The simplest solution would be to place the content you want as an excerpt in the dedicated excerpt field of the editor. If it’s not visible, edit your screen options to make it visible. You can place anything there you want as an excerpt, it doesn’t even need to reflect the post’s content.

    Thread Starter emot1onz

    (@emot1onz)

    Thanks for your answer @bcworkz. You are right, i use a render_callbacks for my blocks. But i also save the InnerBlocks, thats why i dont understand that the_excerpt cant “see” the content:

    export default function save() {
        return <InnerBlocks.Content />
    }
    

    I now solved the problem like this:

    $excerpt = wp_trim_excerpt($post->post_content);
    $clean_excerpt = wp_strip_all_tags($excerpt);
    echo substr($clean_excerpt, 0, 200).'...';

    Any improvement tips are welcome!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘the_excerpt doesn’t work with custom blocks’ is closed to new replies.