2017-09-12 22 views

答えて

0

独自のAPIエンドポイントを作成したことがありますか? https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

あなたのエンドポイントがhttp://example.com/wp-json/myplugin/v1/post-titlesなっところ、このような何かはおそらくあなたのために働くだろう:

function my_awesome_func($data) { 
    $args = array(
    'post_type' => 'post', 
    ); 
    $query = new WP_Query($args); 

    $arr = array(); 
    while ($query->have_posts()) { 
     $query->the_post(); 
     $titles = get_the_title(); 
     array_push($arr, $titles); 
    } 
    return $arr; 
    } 

    add_action('rest_api_init', function() { 
    register_rest_route('myplugin/v1', '/post-titles', array(
     'methods' => 'GET', 
     'callback' => 'my_awesome_func', 
    )); 
    }); 
+0

私は、私たち自身のプラグインでこれを使用することはできますか?これにアクセスするための正確なURLは何でしょうか? –

関連する問題