Skip to content

Instantly share code, notes, and snippets.

@lukasfarina
Created February 22, 2016 17:12
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 lukasfarina/f6f06a56db691b7a37cc to your computer and use it in GitHub Desktop.
Save lukasfarina/f6f06a56db691b7a37cc to your computer and use it in GitHub Desktop.
Pega posts de um blog usando WP Rest Api junto da Thumbnail
<?php
function get_recent_posts($per_page = 2) {
// featured_media
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_SERVER['SERVER_NAME'] . "/blog/wp-json/wp/v2/posts?per_page={$per_page}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$result = json_decode($output, true);
curl_close($ch);
//
if(is_array($result) && !empty($result)):
for($i=0; $i< count($result); $i++) {
$result[$i]['featured_image_url'] = get_media($result[$i]["featured_media"]);
}
return $result;
else:
return false;
endif;
}
function get_media($id) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_SERVER['SERVER_NAME'] . "/blog/wp-json/wp/v2/media/{$id}");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$result = json_decode($output, true);
curl_close($ch);
if(is_array($result) && !empty($result)):
if(isset($result["media_details"]["sizes"]["thumbnail"])):
return $result["media_details"]["sizes"]["thumbnail"]["source_url"];
else:
return $result["guid"]["rendered"];
endif;
else:
return false;
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment