WordPress:在 "the_content" 过滤器中加载另一个 page/post 会导致 500 内部服务器错误

WordPress: Loading another page/post inside the "the_content" filter creates an 500 Internal Server Error

我想用“the_content”过滤器修改 posts 的内容。 在这个过滤器中,我想加载另一个 posts 内容。

这是我的代码:

add_filter( 'the_content', 'my_content_filter' );

function my_content_filter( $content ){

    if ( !is_singular( 'my-custom-post-type' ) ){
        return $content;
    }

    ob_start();

    echo '<p>SOME HTML BEFORE THE CONTENT</p>';

    echo $content;

    echo '<p>SOME HTML AFTER THE CONTENT</p>';

    $module = get_post( 12345 ); // load specific post
    echo apply_filters( 'the_content', $module->post_content );

    echo '<p>SOME MORE HTML</p>';

    $html = ob_get_contents();
    ob_end_clean();

    return $html;

}

遗憾的是,这会造成 500 内部服务器错误。 我猜是因为我创建了一个无限循环。您知道如何在“the_content”过滤器中获取另一个 post 的格式化内容吗?

谢谢:-) 一月

编辑

更多细节:我创建了一个名为“侧边栏”的自定义 post 类型,我在其中使用页面构建器编辑内容。我想用 PHP.

添加这个侧边栏

解决方案

使用“Elementor”页面构建器,您可以这样做:

add_filter( 'the_content', 'my_content_filter' );

function my_content_filter( $content ){

    if ( !is_singular( 'my-custom-post-type' ) ){
        return $content;
    }

    ob_start();

    echo '<p>SOME HTML BEFORE THE CONTENT</p>';

    echo $content;

    echo '<p>SOME HTML AFTER THE CONTENT</p>';

    $elementor = \Elementor\Plugin::instance();
    echo $elementor->frontend->get_builder_content( 12345 );

    echo '<p>SOME MORE HTML</p>';

    $html = ob_get_contents();
    ob_end_clean();

    return $html;

}