Created
February 22, 2016 17:12
-
-
Save lukasfarina/f6f06a56db691b7a37cc to your computer and use it in GitHub Desktop.
Pega posts de um blog usando WP Rest Api junto da Thumbnail
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 | |
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