首页 > 解决方案 > 如何在 php 页面中插入块代码?

问题描述

我的站点中有这个 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"} /-->

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

标签: phpwordpresswordpress-gutenberggutenberg-blocks

解决方案


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

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

    ?>

推荐阅读