首页 > 解决方案 > Wordpress REST API:获取原始数据库内容

问题描述

我想从我的事件日历中提取数据。所以我为我的事件内容类型添加了 REST API 支持。但我的 JSON 输出是 HTML。

我不能获取原始数据库数据吗?

我明白了:http ://astc.dk/wp-json/wp/v2/event/

我把它放在我的functions.php中

add_action( 'init', 'my_event_cpt', 25 );
function my_event_cpt() {
  global $wp_post_types;

  //be sure to set this to the name of your post type!
  $post_type_name = 'event';
  if( isset( $wp_post_types[ $post_type_name ] ) ) {
    $wp_post_types[$post_type_name]->show_in_rest = true;
    // Optionally customize the rest_base or controller class
    $wp_post_types[$post_type_name]->rest_base = $post_type_name;
    $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
  }
}

标签: wordpressapi

解决方案


推荐阅读