加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 使用另一个标签为WordPress RSS添加缩略图

发布时间:2020-12-13 21:46:21 所属栏目:PHP教程 来源:网络整理
导读:我正在使用Wordpress RSS在我的iOS项目中使用. Feed中没有缩略图链接,因此我搜索并找到此代码以添加缩略图链接到Feed. /* include thumbnail in RSS feed */function add_thumb_to_RSS($content) { global $post; if ( has_post_thumbnail( $post-ID ) ){ $c
我正在使用Wordpress RSS在我的iOS项目中使用. Feed中没有缩略图链接,因此我搜索并找到此代码以添加缩略图链接到Feed.

/* include thumbnail in RSS feed */
function add_thumb_to_RSS($content) {
   global $post;
   if ( has_post_thumbnail( $post->ID ) ){
      $content = '' . get_the_post_thumbnail( $post->ID,'thumbnail' ) . '' . $content;
   }
   return $content;
}
add_filter('the_excerpt_rss','add_thumb_to_RSS');
add_filter('the_content_feed','add_thumb_to_RSS')

此代码在Feed中添加了图片链接,但它在描述标记的开头添加了html代码,如下所示:

<description>
<![CDATA[
<img width="150" height="150" src="http://www.ipadia.co/wp-content/uploads/2012/02/sayfada-bul-150x150.png" class="attachment-thumbnail wp-post-image" alt="sayfada bul" title="sayfada bul" />Some text some text Some text some text Some text some text Some text some text Some text some text Some text some text ....
]]>
</description>

我想添加图像链接与另一个标签,如< image>或者< thumb>链接< / thumb>.所以我可以更容易地解析它.

我怎样才能做到这一点?提前致谢.

解决方法

我终于解决了:)我改变了之前发布的功能.新功能是这样的:

add_action('rss2_item',function(){
  global $post;

  $output = '';
  $thumbnail_ID = get_post_thumbnail_id( $post->ID );
  $thumbnail = wp_get_attachment_image_src($thumbnail_ID,'thumbnail');
  $output .= '<post-thumbnail>';
    $output .= '<url>'. $thumbnail[0] .'</url>';
    $output .= '<width>'. $thumbnail[1] .'</width>';
    $output .= '<height>'. $thumbnail[2] .'</height>';
    $output .= '</post-thumbnail>';

  echo $output;
});

这样就可以在新标签中显示图像链接< post-thumbnail>就像我想要的那样

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读