个性化阅读
专注于IT技术分析

自定义端点API,可通过按单词返回False值获取所有主题

点击下载

这是我在此处创建的用于获取所有主题的自定义终点。但是在json中, 它没有返回预期的结果。

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()函数, 它将以数组形式返回所有主题及其描述。并且它在数组中返回很好, 但是当我将其编码为json以传递数据时, 它仅返回数组键。

像这样只产生键名

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

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

我该如何使用自定义REST端点。

请帮忙。


#1


试试这个代码

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;
}
赞(0)
未经允许不得转载:srcmini » 自定义端点API,可通过按单词返回False值获取所有主题

评论 抢沙发

评论前必须登录!