Skip to content

Instantly share code, notes, and snippets.

@andredublin
Created October 4, 2011 23:18
Show Gist options
  • Save andredublin/1263132 to your computer and use it in GitHub Desktop.
Save andredublin/1263132 to your computer and use it in GitHub Desktop.
Wordpress loop that assign an associative array to an array then echo json
define('WP_USE_THEMES', false);
require($_SERVER['DOCUMENT_ROOT'] . '/wp-blog-header.php');
header('HTTP/1.1 200 OK');
header('Content-type: application/json;');
$posts = array();
if (have_post()) :
while (have_posts()) :
$posts = the_post();
$posts['id'] = the_ID();
$posts['title'] = the_title();
$results[] = $posts;
endwhile;
echo json_encode($results);
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment