Last active
March 25, 2022 12:24
-
-
Save web-elite/f6b617a5d529337944cd7157c8e1d007 to your computer and use it in GitHub Desktop.
get post from rest api with full information ,
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 | |
/** | |
* Add a Formatted Date to the WordPress REST API JSON Post Object | |
*/ | |
add_action('rest_api_init', function () { | |
register_rest_field( | |
'post', | |
'formatted_date', | |
array( | |
'get_callback' => function () { | |
return get_the_date(); | |
}, | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
//custom field for post | |
register_rest_field( | |
'post', | |
'reading_time', | |
array( | |
'get_callback' => function () { | |
return get_post_meta(get_the_ID(), 'reading_time', true); | |
}, | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
//custom field for post | |
register_rest_field( | |
'post', | |
'video_time', | |
array( | |
'get_callback' => function () { | |
return get_post_meta(get_the_ID(), 'video_time', true); | |
}, | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
}); | |
//get new post per 1 hours | |
define('HOURS', 60 * 60 * 12); | |
function get_remote_api_data($id = '') | |
{ | |
global $apiData; | |
if (empty($apiData)) $apiInfo = get_transient('api_data'); | |
if (!empty($apiData)) return $apiData; | |
if (empty($id)) { | |
$response = wp_remote_get('https://hooshtejari.com/blog/wp-json/wp/v2/posts?per_page=4&_embed', array( | |
'timeout' => 20, | |
)); | |
} else { | |
$response = wp_remote_get('https://hooshtejari.com/blog/wp-json/wp/v2/posts?' . $id . '&_embed', array( | |
'timeout' => 20, | |
)); | |
} | |
$data = wp_remote_retrieve_body($response); | |
if (empty($data)) return false; | |
$apiData = json_decode($data); | |
set_transient('api_data', $apiData, HOURS); | |
return $apiInfo; | |
} | |
function get_post_from_api($atts = "") | |
{ | |
$value = shortcode_atts(array( | |
'id' => '', | |
), $atts); | |
echo '<div class="posts-wrapper">'; | |
if (empty($value['id'])) { | |
$feed = get_remote_api_data(); | |
} else { | |
$feed = get_remote_api_data($value['id']); | |
} | |
if ($feed) { | |
foreach ($feed as $post) { | |
$singleTerm = ''; | |
$title = $post->title->rendered; //get title | |
$excerpt = $post->excerpt->rendered; //get excerpt | |
if (!empty($post->_embedded->{'wp:featuredmedia'}[0]->source_url)) { | |
$image = $post->_embedded->{'wp:featuredmedia'}[0]->source_url; //get feature iamge | |
$altText = $post->_embedded->{'wp:featuredmedia'}[0]->alt_text; //get feature iamge alt text | |
} else { | |
$image = 'https://picsum.photos/350/400'; // this url for post has not image. | |
} | |
$terms = $post->_embedded->{'wp:term'}[0]; //get terms --> [0] = category and [1] = tags | |
$author = $post->_embedded->{'author'}[0]; //get author | |
if (!empty($post->_embedded->{'replies'}[0])) { | |
$comments = count($post->_embedded->{'replies'}[0]); //get number of comments | |
} else { | |
$comments = 0; | |
} | |
foreach ($terms as $term) { | |
$singleTerm .= "<a id='{$term->id}' href='{$term->link}'>{$term->name}</a>"; | |
} | |
echo "<div id='{$post->id}' class='single-post' style='background-image: url({$image})' > | |
<a href='{$post->link}' class='post-link'></a> | |
<div class='post-content'> | |
<div class='post-terms'>{$singleTerm} </div> | |
<a href='{$post->link}' class='post-title'>{$title}</a> | |
<div class='post-meta'> | |
<a href='{$post->link}'><i class='far fa-calendar-alt'></i>{$post->formatted_date}</a> | |
<a href='{$author->link}' class='post-author'><i class='far fa-user-circle'></i>{$author->name}</a> | |
<a href='{$post->link}#comments' class='post-comments'><i class='far fa-comments'></i>{$comments}</a> | |
</div> | |
</div> | |
</div>"; | |
} | |
} | |
echo '</div>'; | |
} | |
//Use this shortcode --> [progy_show_posts] | |
add_shortcode("progy_show_posts", "get_post_from_api"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment