Support » Plugin: Gutenberg » Extract a Gutenberg block for the excerpt

  • Resolved brendaegeland

    (@brendaegeland)


    I would like to have my excerpts be a particular GB block extracted from the post content, rather than the first x characters or whatever. Is this possible? I could just get the content and filter it somehow, but I’m wondering if there’s a function to do this already? Perhaps I’m just not searching for the right thing.

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You would need to create custom code to make the system automatically select a different kind of excerpt.

    Alternatively, you can simply copy and paste the desired content in to the manual Excerpt field, available in the right hand sidebar of the editor.

    brendaegeland

    (@brendaegeland)

    Thank you for the reply. I don’t think I can trust my users to remember to create the manual excerpt each time, so I’d like to automate it. I can see how to code a custom excerpt, I’m just not seeing how to extract a block from the content, or perhaps more likely the first instance of a particular block type. Do I just grab the html for the content and parse it? Or is there a function available to do that already?

    Thank you.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You would need to take the HTML code from the content and then parse it however you wanted. Gutenberg leaves HTML comments in there surrounding the blocks, so you could look for those if you wanted to find the first block of a particular type.

    brendaegeland

    (@brendaegeland)

    Thanks, I’ll give it a try. Might even end up as a plug-in someday!

    brendaegeland

    (@brendaegeland)

    I was able to use the gutenberg_parse_blocks function to get the blocks I needed. Ended up being pretty simple:

    function d6bridge_filter_for_results($content){
        if (is_archive('gnt-result') || is_archive('nap-result')) {
      	if ( gutenberg_content_has_blocks( $content ) ) {
          	    $blocks = gutenberg_parse_blocks( $content );
                $content = '';
      	    foreach ($blocks as $block) {
                    if ($block->blockName == 'rsvpbridge/competition-result') {
                        $content .= $block->innerHTML;
                    }
      	    }
      	}
        if ($content == '') {
          $content = '<p>Results not posted</p>';
        }
      }
      return $content;
    }
    add_filter('the_content', 'd6bridge_filter_for_results', 1);
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Extract a Gutenberg block for the excerpt’ is closed to new replies.