Last active
October 23, 2018 18:39
-
-
Save PeterBooker/d9737151466db503c376e0ecb8751911 to your computer and use it in GitHub Desktop.
WP REST API Examples
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
<?php | |
/** | |
* Get Public Posts from External Site using REST API | |
*/ | |
$hostname = 'https://www.peterbooker.com'; | |
$num = 5; | |
$response = wp_remote_get( | |
add_query_arg( | |
array( | |
'per_page' => $num, | |
), | |
$hostname . '/wp-json/wp/v2/posts' | |
) | |
); | |
if ( ! is_wp_error( $response ) && 200 === $response['response']['code'] ) { | |
$articles = json_decode( $response['body'] ); | |
foreach ( $articles as $article ) { | |
echo '<h2>' . esc_html( $article->title->rendered ) . '</h2>'; | |
echo '<p>' . wp_kses_post( $article->excerpt->rendered ) . '</p>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment