-2

I am trying to develop a small WordPress plugin to fetch the posts, pages from the website and parse it as json to further use in mobile apps. Right now I am achieving the goal via this method:

1) Created a file webservice.php on my current active theme eg. twentythirteen. So the location of the file is:

http://www.example.com/wp-content/themes/twentythirteen/webservice.php

2) I am posting the parameters on that URL to get a JSON response like this

http://www.example.com/wp-content/themes/twentythirteen/webservice.php?type=page&limit=10

The thing is I want to post parameters on the home page like this:

http://www.example.com?type=page&limit=10 

I don't know how to do it but I have seen the JSON API plugin which is doing the same thing but I'm not able to find in the code how it's fetching the request from the home page and parse JSON on the same page. How can I do this?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Harish Kumar
  • 927
  • 3
  • 20
  • 46

1 Answers1

0

I developed a WordPress plugin and I'm using it for my PhoneGap app, but it may also help you. This is the code for the callback last posts:

header('Content-Type: application/json');
require('../../../wp-load.php');
require('../../../wp-includes/pluggable.php');

$post = "";
$elementos = 5; //Number of Post
$yaCargados = 0;
global $wpdb;
if($_POST['num_post']!=0 or $_POST['num_post']!="NULL") {
$elementos = $_POST['num_post'];
$yaCargados = $_POST['paginacion'];
}
$args = array(
    'posts_per_page'    => $elementos,
    'offset'           => $yaCargados,
    'orderby'          => 'post_date',  
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$posts_array = get_posts( $args );
if(0 < $posts_array) {
foreach( $posts_array as $term) {
$res['posts'][] = $term;    
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $term->ID ), 'medium' );
$res['images'][]['imagen'] = $image;
$custom_fields = get_post_custom($term->ID);
 $res['custom_field'][] = $custom_fields;
}
echo json_encode($res);
}else{

}

Save archive in /wp-content/plugins/[YOUPLUGIN] and call and print post in JSON format.

Happy Coding!

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127