Skip to content

Instantly share code, notes, and snippets.

@PeterBooker
Last active October 23, 2018 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBooker/d9737151466db503c376e0ecb8751911 to your computer and use it in GitHub Desktop.
Save PeterBooker/d9737151466db503c376e0ecb8751911 to your computer and use it in GitHub Desktop.
WP REST API Examples
<?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