免费《新手做网站视频教程》,在线观看,如果你想从零开始自己做网站,点击开始吧!

WordPress获取文章中第一张图片代码

  1. 首页
  2. »
  3. 图文教程
  4. »
  5. WordPress建站教程
  6. »
  7. WordPress获取文章中第一张图片代码

文章目录

// 获取文章中第一张图片,返回值:图片url地址// 获取文章中第一张图片,返回值:图片url地址
public function get_post_first_image_url() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
$first_img = $matches[1][0];

if(empty($first_img)) {
$first_img = get_bloginfo('template_url') . '/static/picture/get_post_first_image_url_default.gif';
}
return $first_img;
}

 

get_post_first_image_url_default.gif 是文章中没有图片默认显示的图片。

前台主题调用使用 <?php echo get_post_first_image_url();?>

推荐阅读

联系我