自定义端点 API 从 word-press 返回 False 值获取所有主题

Custom End points API to get all themes from word-press returning False value

这是我在这里创建的用于获取所有主题的自定义端点。但是在 json 中,它并不是 return 预期的结果。

add_action( ‘rest_api_init’, function () {
//Path to rest endpoint
register_rest_route( ‘theme/v1’, ‘/get_theme_list/’, array(
‘methods’ => ‘GET’,
‘callback’ => ‘theme_list_function’
) );
});
// Our function to get the themes
function theme_list_function(){
// Get a list of themes
$list = wp_get_themes();
// Return the value
return $list;
}

?>

如果我能简单地看到 wp_get_themes() 函数,它将 return 数组中的所有主题及其描述。 return 在数组中很好,但是当我将其编码为 json 以传递数据时,它的 return 仅数组键。

像这样只生成键名

All: {"basepress":{"update":false},"codilight-lite":{"update":false},"twentyfifteen":{"update":false},"twentyseventeen":{"update":false},"twentysixteen-child":{"update":false},"twentysixteen":{"update":false}}

我需要有关主题的所有信息。

我如何使用自定义 REST 端点来做到这一点。

请帮忙。

试试这个代码

add_action( 'rest_api_init', function () {
//Path to rest endpoint
    register_rest_route( 'theme/v1', '/get_theme_list/', array('methods' => 'GET','callback' => 'theme_list_function') );
});
// Our function to get the themes
function theme_list_function(){
    // Get a list of themes
    $list = wp_get_themes();

    $varTheme = array();

    foreach($list as $theme=>$value){
        $varTheme[$theme] = (array)$value;
    }
    return $varTheme;
}