admin管理员组文章数量:1122832
I have a custom function that pulls in (3) posts from another Wordpress blog, and it's pulling them from specific categories I specify via a shortcode.
Now, with that said, I cannot get it to function and display the actual image, as I can only get it to show the featured Image's (media attachment's) ID itself.
Here is my functional code, which works except the image is not displayed, instead it's the images ID (numerical value)
function feedpuller_func($atts)
{
$url = $atts['url'];
$count = ($atts['count'])?$atts['count']:'3';
if($atts['catid'])
{
$cat = '?categories='.$atts['catid'].'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/?_embed'.$cat );
}
else if ($atts['slug'])
{
$slug = $atts['slug'];
echo $url . '/wp-json/wp/v2/categories?slug='.$slug;
$responseslug = wp_remote_get($url . '/wp-json/wp/v2/categories?slug='.$slug );
$postdata = json_decode( wp_remote_retrieve_body( $responseslug ) );
if($postdata)
$catid = $postdata[0]->id;
else
echo 'no record against this slug';
if($catid)
{
$cat = '?categories='.$catid.'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/?_embed'.$cat );
}
}
else
$cat = '';
if( is_wp_error( $response ) ) {
return;
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $posts ) ) {
return;
}
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
if( !empty( $posts ) ) {
echo '<ul class="feedothersite">';
foreach( $posts as $post ) { //echo '<pre>'; print_r($post);
echo '<li><a href="' . $post->link . '">' . '<img src="' . $post->featured_media . '">' . '<br>'. $post->title->rendered .'<br>'. $post->excerpt->rendered . '</a></li>';
}
echo '</ul>';
}
}
add_shortcode("feedpuller","feedpuller_func");
EDIT: I do NOT want to use a plugin already made, and also I have tried the solutions at the related post, but my functions differ because I am displaying posts from certain categories, and typical posts are pulled and displayed without these parameters.
Suggested Method (I know this works for regular posts)
/?rest_route=/wp/v2/posts&_embed
/wp-json/wp/v2/posts?_embed
But mine are called like this:
/wp-json/wp/v2/categories?slug='.$slug
I have a custom function that pulls in (3) posts from another Wordpress blog, and it's pulling them from specific categories I specify via a shortcode.
Now, with that said, I cannot get it to function and display the actual image, as I can only get it to show the featured Image's (media attachment's) ID itself.
Here is my functional code, which works except the image is not displayed, instead it's the images ID (numerical value)
function feedpuller_func($atts)
{
$url = $atts['url'];
$count = ($atts['count'])?$atts['count']:'3';
if($atts['catid'])
{
$cat = '?categories='.$atts['catid'].'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/?_embed'.$cat );
}
else if ($atts['slug'])
{
$slug = $atts['slug'];
echo $url . '/wp-json/wp/v2/categories?slug='.$slug;
$responseslug = wp_remote_get($url . '/wp-json/wp/v2/categories?slug='.$slug );
$postdata = json_decode( wp_remote_retrieve_body( $responseslug ) );
if($postdata)
$catid = $postdata[0]->id;
else
echo 'no record against this slug';
if($catid)
{
$cat = '?categories='.$catid.'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/?_embed'.$cat );
}
}
else
$cat = '';
if( is_wp_error( $response ) ) {
return;
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $posts ) ) {
return;
}
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
if( !empty( $posts ) ) {
echo '<ul class="feedothersite">';
foreach( $posts as $post ) { //echo '<pre>'; print_r($post);
echo '<li><a href="' . $post->link . '">' . '<img src="' . $post->featured_media . '">' . '<br>'. $post->title->rendered .'<br>'. $post->excerpt->rendered . '</a></li>';
}
echo '</ul>';
}
}
add_shortcode("feedpuller","feedpuller_func");
EDIT: I do NOT want to use a plugin already made, and also I have tried the solutions at the related post, but my functions differ because I am displaying posts from certain categories, and typical posts are pulled and displayed without these parameters.
Suggested Method (I know this works for regular posts)
/?rest_route=/wp/v2/posts&_embed
/wp-json/wp/v2/posts?_embed
But mine are called like this:
/wp-json/wp/v2/categories?slug='.$slug
Share
Improve this question
edited Mar 15, 2018 at 1:29
Michael G
asked Mar 15, 2018 at 0:18
Michael GMichael G
31 silver badge6 bronze badges
2
- Possible duplicate of WP Rest API - How to get featured image – Jacob Peattie Commented Mar 15, 2018 at 0:55
- I reviewed that post unfortunately I am not able to correctly process the functions with the way I am also pulling the specific category posts. I spent probably two hours on that post, trying different things too. thanks though. – Michael G Commented Mar 15, 2018 at 1:23
1 Answer
Reset to default 0I have found the solution with the help of a great friend. I will post it here if anyone else needs to utilize the fucntion. Again in summary, this function will display posts from another blog and you can enter specific category ID's from the other blog and also control how many posts are displayed.
function feedpuller_func($atts)
{
$url = $atts['url'];
$count = ($atts['count'])?$atts['count']:'3';
$imgsize = (in_array($atts['img'], array('thumbnail','medium','large','full')))?$atts['img']:'thumbnail';
if($atts['catid'])
{
$cat = '?_embed&categories='.$atts['catid'].'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/'.$cat );
}
else if ($atts['slug'])
{
$slug = $atts['slug'];
echo $url . '/wp-json/wp/v2/categories?slug=' . $slug;
$responseslug = wp_remote_get($url . '/wp-json/wp/v2/categories?slug=' . $slug);
$postdata = json_decode( wp_remote_retrieve_body( $responseslug ) );
if($postdata)
$catid = $postdata[0]->id;
else
echo 'no record against this slug';
if($catid)
{
$cat = '?categories='.$catid.'&per_page='.$count;
$response = wp_remote_get( $url.'/wp-json/wp/v2/posts/'.$cat );
}
}
else
$cat = '';
if( is_wp_error( $response ) ) {
return;
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if( empty( $posts ) ) {
return;
}
if( !empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
if( !empty( $posts ) ) {
echo '<ul class="feedothersite">';
foreach( $posts as $post ) { //echo '<pre>'; print_r(); exit;
$imgurl = $post->_embedded->{'wp:featuredmedia'}[0]->media_details->sizes->$imgsize->source_url;
if($imgurl)
$img = '<img src="'.$imgurl.'">';
else
$img = '';
echo '<div class="feedcontent"><a href="' . $post->link. '"><li><div class="feedimg">'.$img.'<div><h2>' . $post->title->rendered . '</h2></a></div></li>';
}
echo '</ul>';
}
}
add_shortcode("feedpuller","feedpuller_func");
Shortcode value should follow [feedpuller url="http://othersite.com/" catid="22" count="4"] To display featured image, use: img="full" img="medium" or img="thumbnail"
本文标签: jsonDisplay Post Featured Image along with Categories via WP Rest API
版权声明:本文标题:json - Display Post Featured Image along with Categories via WP Rest API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736286546a1927691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论