Created
March 11, 2015 02:52
-
-
Save flashvnn/e438045cf11869ec799e to your computer and use it in GitHub Desktop.
Wordpress Post to JSON
This file contains hidden or 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
| <?php | |
| // include our wordpress functions | |
| // change relative path to find your WP dir | |
| define('WP_USE_THEMES', false); | |
| require('./blog/wp-blog-header.php'); | |
| // set header for json mime type | |
| header('Content-type: application/json;'); | |
| // get latest single post | |
| // exclude a category (#5) | |
| query_posts(array( | |
| 'posts_per_page' => 1, | |
| 'cat' => -5, | |
| )); | |
| $jsonpost = array(); | |
| if (have_posts()) { | |
| // initialize functions used below | |
| the_post(); | |
| // construct our array for json | |
| // apply_filters to content to process shortcodes, etc | |
| $jsonpost["id"] = get_the_ID(); | |
| $jsonpost["title"] = get_the_title(); | |
| $jsonpost["url"] = apply_filters('the_permalink', get_permalink()); | |
| $jsonpost["content"] = apply_filters('the_content', get_the_content()); | |
| // would rather do iso 8601, but not supported in gwt (yet) | |
| $jsonpost["date"] = get_the_time('d F Y'); | |
| } else { | |
| // deal with no posts returned | |
| } | |
| // output json to file | |
| echo json_encode($jsonpost); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment