Skip to content

Instantly share code, notes, and snippets.

@gwhitcher
Created April 20, 2018 15:35
Show Gist options
  • Save gwhitcher/ed858927d788d6d524f7be9d0107c6fc to your computer and use it in GitHub Desktop.
Save gwhitcher/ed858927d788d6d524f7be9d0107c6fc to your computer and use it in GitHub Desktop.
<?php
function getWordpressPosts() {
$wordpressUrl = 'http://domain.com/';
$endpoint = '/wp/v2/posts/';
$vars = '';
$curl = curl_init( $wordpressUrl . 'wp-json' . $endpoint.$vars );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
) );
$response = curl_exec( $curl );
$values = json_decode($response, true);
$data = '';
foreach($values as $key => $value) {
$data[] = [
'title' => $value['title']['rendered'],
'url' => $value['link']
];
}
return $data;
}
function getWordpressProducts() {
$wordpressUrl = 'http://domain.com/';
$endpoint = '/wp/v2/product/';
$vars = '';
$curl = curl_init( $wordpressUrl . 'wp-json' . $endpoint.$vars );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC
) );
$response = curl_exec( $curl );
//print_r($response);
$values = json_decode($response, true);
$data = '';
foreach($values as $key => $value) {
$data[] = [
'title' => $value['title']['rendered'],
'url' => $value['link']
];
}
return $data;
}
?>
<h2>Recent Posts</h2>
<ul>
<?php
$posts = getWordpressPosts();
foreach($posts as $post) {
echo '<li><a href="'.$post['url'].'">'.$post['title'].'</a></li>';
}
?>
</ul>
<h2>Recent Products</h2>
<ul>
<?php
$posts = getWordpressProducts();
foreach($posts as $post) {
echo '<li><a href="'.$post['url'].'">'.$post['title'].'</a></li>';
}
?>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment