当我从 wp rest api 调用中获取内容时,简码不起作用

shortcode doesn't work when I'm getting content from wp rest api call

我正在尝试使用 wp rest api 获取名为事件的自定义 post 类型的内容。一切正常,但我使用 Wp Backery 页面构建器在我的事件 post 类型中创建内容,问题是我在没有解析的情况下获得短代码,它们是纯文本。

有人可以帮我解决这个问题吗?

我尝试了一些类似问题的答案,例如使用 do_shortcode() 函数并尝试调用 apply_filters( 'the_content' , get_the_content()) 但是 none 对我有用

这是我的休息-api代码:

add_action( 'rest_api_init' , 'wt_rest_api');

function wt_rest_api(){
    register_rest_route('wtrest','events',array(
            'methods'   => WP_REST_SERVER::READABLE,
            'callback'  => 'wtEventResults'
        )); 
}

function wtEventResults($data){
    $events = new WP_Query([
        'post_type' => 'event',
        'post__in'  => array( (int)$data['id'] )
        ]);

    $eventsResults = [];

    while($events->have_posts()){
        $events->the_post();

        array_push($eventsResults , [
            'content'   => apply_filters( 'the_content' , get_the_content())
            ]);
    }

    return $eventsResults;

}

我得到了结果,但是短代码没有被解析,它们只是纯文本。 提前致谢

感谢 @muka.gergely I've found the answer here

对我有用的最终代码在这里:

add_action( 'rest_api_init' , 'wt_rest_api');

function wt_rest_api(){
    register_rest_route('wtrest','events',array(
            'methods'   => WP_REST_SERVER::READABLE,
            'callback'  => 'wtEventResults'
        )); 
}


function wtEventResults($data){
    WPBMap::addAllMappedShortcodes(); // This does all the work

    $events = new WP_Query([
        'post_type' => 'event',
        'post__in'  => array( (int)$data['id'] )
        ]);

    $eventsResults = [];

    while($events->have_posts()){
        $events->the_post();


        array_push($eventsResults , [
                'content'   => apply_filters( 'the_content' ,  get_the_content() )
            ]);
    }

    return $eventsResults;

}

希望它能为您节省一些时间

对于任何可能感兴趣的人,这对我有用

apply_filters('the_content', $content);

也是here