Search code examples
phpwordpresswordpress-gutenberggutenberg-blocks

How can I insert a block code inside a php page?


I have this front-page.php page in my site and I want to put a block code directly in the php page. So I got a div and I want to put it in there. But the code is like

<!-- wp:latest-posts {"postsToShow":3,"displayPostContent":true,"excerptLength":30,"displayPostDate":true,"postLayout":"grid","displayFeaturedImage":true,"featuredImageSizeSlug":"large","align":"wide","className":"tw-mt-8 tw-img-ratio-3-2 tw-stretched-link is-style-default"} /-->

It's from a widget. I coudn't make it work please help :D


Solution

  • You can insert block code into any php template by using the function do_blocks() and passing in (valid) block content as a string, eg:

        <?php
    
        $block_content = '<!-- wp:latest-posts {"postsToShow":3,"displayPostContent":true,"excerptLength":30,"displayPostDate":true,"postLayout":"grid","displayFeaturedImage":true,"featuredImageSizeSlug":"large","align":"wide","className":"tw-mt-8 tw-img-ratio-3-2 tw-stretched-link is-style-default"} /-->';
        
        echo do_blocks($block_content);
    
        ?>