首页 > CMS > Wordpress > 正文

如何获取文章的第一张图片

2024-09-07 00:50:48
字体:大 中 小
来源:转载
供稿:网友

首先在当前模板中的function.php中写入:

  1. function catch_that_image() { 
  2.   global $post, $posts; 
  3.   $first_img = ''; 
  4.   ob_start(); 
  5.   ob_end_clean(); 
  6.   $output = preg_match_all('//i', $post->post_content, $matches); 
  7.   $first_img = $matches [1] [0]; 
  8.  
  9.   if(emptyempty($first_img)){ //Defines a default image 
  10.     $first_img = "/images/default.jpg"; 
  11.   } 
  12.   return $first_img; 
  13. } 

然后在需要调用的地方写上.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表