Skip to content

Instantly share code, notes, and snippets.

@gabhi
Created March 26, 2014 09:06
Show Gist options
  • Save gabhi/9779286 to your computer and use it in GitHub Desktop.
Save gabhi/9779286 to your computer and use it in GitHub Desktop.
php wordpress api -- json conversion
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
require_once('wp-load.php');
$response = array();
if(isset($_GET['post_id'])) {
$args = array(
'ID' => $_GET['post_id']
);
$mypost = get_post($_GET['post_id']);
//echo json_encode($root_categories);
$response['mypost'] =$mypost;
} else if(isset($_GET['id'])) {
// id index exists
if($_GET['id']==0){
$root_categories = get_categories( array(
'parent' => 0,
) );
//echo json_encode($root_categories);
$response['cat'] =$root_categories;
}
else{
$args = array(
'child_of' => $_GET['id']
);
$root_categories = get_categories($args);
//echo json_encode($root_categories);
$response['cat'] =$root_categories;
$args = array( 'posts_per_page' => 15, 'offset'=> 1, 'category' => $_GET['id'] );
$myposts = get_posts( $args );
$response['myposts']=$myposts;
//echo json_encode($myposts);
}
}
echo json_encode($response);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment