我有一个自定义函数,可以从另一个Wordpress博客中提取(3)篇文章,它通过一个快捷码从我指定的特定类别中提取它们。
现在,尽管如此,我无法让它运行并显示实际图像,因为我只能让它显示特色图像(媒体附件)的ID本身。
这是我的功能代码,除了不显示图像外,它可以工作,而是图像ID(数值)
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");
编辑:我不想使用已经制作好的插件,我也在相关帖子上尝试过解决方案,但我的功能不同,因为我显示的是特定类别的帖子,典型的帖子都是在没有这些参数的情况下拉出来显示的。
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
SO网友:Michael G
在一位好朋友的帮助下,我找到了解决办法。如果其他人需要使用这个功能,我会把它贴在这里。总之,此功能将显示来自其他博客的帖子,您可以输入来自其他博客的特定类别ID,还可以控制显示的帖子数量。
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");
快捷码值应在[feedpuller url=”之后http://othersite.com/“catid=”22“count=”4“]要显示特色图像,请使用:img=“full”img=“medium”或img=“缩略图”