0

How would you explain this to a newbie?

eg.: I have two websites, I want one to display the posts of the other through JSON API?

  • Does it need to be JSON? there is a xml feed present already, anyways if you see in right sidebar here in "Related" you can find a question with code for a JSON output of posts already. – David Lee Feb 3 '17 at 19:42
0

I dont know how to edit question, I found this: but this way I cannot specify my requests, is there any way to modify this code so that I can specify it?

 function check_api_data() {    
  if(isset($_GET['api']) ) {
  if ($_GET['api'] == 'json'){
           $args = array(  
               'post_status' => 'publish', 
               'nopaging'    => true,
               'orderby'     => 'title',
               'order'       => 'ASC'
           );
           $query = new WP_Query( $args ); // $query is the WP_Query Object
           $posts = $query->get_posts();   // $posts contains the post objects

           $output = array();
           foreach( $posts as $post ) {    // Pluck the id and title attributes
               $output[] = array( 
                'id' => $post->ID, 
                'title' => $post->post_title, 
                'content' => $post ->post_content,
                'imageurl' => wp_get_attachment_url( get_post_thumbnail_id($post->ID) )
                );
           }
           echo json_encode( $output );
       }
   exit();
   }  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.