Created
April 12, 2013 00:56
-
-
Save torounit/5368443 to your computer and use it in GitHub Desktop.
WordPressの投稿をJSONで吐き出す。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class WP_JSON_API { | |
public function __construct() { | |
add_action( "init", array( &$this, "add_query_var" ),10); | |
add_action( "init", array( &$this, "json_actions" ),11); | |
} | |
public function add_query_var() { | |
global $wp; | |
$wp->add_query_var( "json" ); | |
} | |
public function json_actions() { | |
if( $_GET["json"] == "true") { | |
add_action( "pre_get_posts", array(&$this, "pre_get_posts")); | |
add_action( "wp", array(&$this, "json_format")); | |
} | |
} | |
public function pre_get_posts( $query ) { | |
$query->set( "nopaging" ,1 ); | |
} | |
public function json_format( $wp ) { | |
global $posts; | |
header( 'Content-Type: application/json' ); | |
echo json_encode( $posts ); | |
exit; | |
} | |
} | |
$jsonapi = new WP_JSON_API; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment