如何在 php 页面中插入块代码?

luc*_*zdj 2 php wordpress wordpress-gutenberg gutenberg-blocks

我的网站中有这个 front-page.php 页面,我想直接在 php 页面中放置一个块代码。所以我有一个 div,我想把它放在那里。但代码就像

<!-- 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"} /-->
Run Code Online (Sandbox Code Playgroud)

它来自一个小部件。我无法让它工作,请帮忙:D

S.W*_*lsh 10

您可以使用该函数do_blocks()并将(有效)块内容作为字符串传递到任何 php 模板中,例如:

    <?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);

    ?>
Run Code Online (Sandbox Code Playgroud)