Support » Plugin: Lazy Blocks - Gutenberg Blocks Constructor » Call Inner Block in Theme

  • Resolved jonroc804

    (@jonroc804)


    Is it possible to call an inner block in my theme? Not the meta of a block but an entire inner block and all it’s content? My use case is that certain blocks would be able to be displayed above/below a hard-coded section in my theme. So for example the standard post content would be at the top, then a hard-coded area in my theme (like a full-width map for example), then below that pull the content inside an inner block.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Actually, I can see how to get it in with this:

    function be_display_post_blockquote() {
      global $post;
      $blocks = parse_blocks( $post->post_content );
      foreach( $blocks as $block ) {
        if( 'lazyblock/area-2' === $block['blockName'] ) {
          echo render_block( $block );
          break;
        }
      }
    }

    But how can I stop the block from showing with the other “standard” blocks?

    jonroc804

    (@jonroc804)

    I figured this out

    
    //If single block exists on page or post don't show it with the other blocks
    function remove_blocks() {
    // Check if we're inside the main loop in a post or page
      if ( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
        //parse the blocks so they can be run through the foreach loop
        $blocks = parse_blocks( get_the_content() );
        foreach ( $blocks as $block ) {
            //look to see if your block is in the post content -> if yes continue past it if no then render block as normal
            if ( 'lazyblock/top-of-page' === $block['blockName'] ) {
                continue;
            } else {
                echo render_block( $block );
            }
        }
      }
    }
    add_filter( 'the_content', 'remove_blocks');
    

    More Detailed Answer Here

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.