Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何获取第一个图像并将其设置为后期缩略图?_Php_Wordpress_Function_Post_Thumbnails - Fatal编程技术网

Php 如何获取第一个图像并将其设置为后期缩略图?

Php 如何获取第一个图像并将其设置为后期缩略图?,php,wordpress,function,post,thumbnails,Php,Wordpress,Function,Post,Thumbnails,我在网上找到了这两个功能,并根据自己的需要进行了编辑。 我要做的是将wordpress帖子的缩略图设置为默认值(先前在函数本身图像中设置),或者设置为嵌入帖子中的第一个图像。 然而,某处出了问题 wptuts\u save\u缩略图($post\u id)->将帖子的缩略图设置为默认值,或者将第一个图像设置为(由帖子作者设置) firstImg($post\u id)->用于获取帖子的第一个图像(按id) 函数firstImg($post\u id){ $post=get\u post($pos

我在网上找到了这两个功能,并根据自己的需要进行了编辑。
我要做的是将wordpress帖子的缩略图设置为默认值(先前在函数本身图像中设置),或者设置为嵌入帖子中的第一个图像。
然而,某处出了问题

wptuts\u save\u缩略图($post\u id)->将帖子的缩略图设置为默认值,或者将第一个图像设置为(由帖子作者设置)

firstImg($post\u id)->用于获取帖子的第一个图像(按id)

函数firstImg($post\u id){
$post=get\u post($post\u id);
$first_img='';
ob_start();
ob_end_clean();
$output=preg_match_all('//i',$post->post_content,$matches);
$first_img=$matches[1][0];
$urlength=strlen(site_url());
$first\u img=substr($first\u img,$urlength);
如果(空($first_img)){
返回false;
}
返回$first\u img;
}
这些函数的唯一问题是
if(!$firstImg)-else语句。
无论帖子中是否嵌入图像,图像都将始终设置为默认值。
$firstImg
确实会返回第一个图像(如果它存在),因此问题必须出现在2个
if
中的任何一个:
if(empty($first\img))
if(!$firstImg)

我试图寻找问题的线索,但什么也没找到

希望有人能对这个问题有所了解:)
提前谢谢

其他信息:
-这两个函数都是在我的主题的
functions.php
中编写的。
-
wptuts\u save\u缩略图($post\u id)
设置为在每次发布新的帖子时运行。

-返回时,
$first\u img
是图像的相对路径(即/wp contents/uploads/img.jpg),或
false

,我可以通过查看代码指出,对firstImg的检查:

if(!$firstImg){ // if available, update the post thumbnail
  update_post_meta( $post_id, '_thumbnail_id', 'link to default image here' );
} else { // else -> set thumbnail to default thumbnail
  update_post_meta( $post_id, '_thumbnail_id', $firstImg );
}
似乎返回false,这将为您提供默认图像

您可以做的是检查转储中的$matches[1][0]的结果,或者在firstImg函数中进行打印。在归还之前,还要检查$first_img是什么。这可以帮助您找到答案,因为您似乎没有在$first\u img中得到预期的结果

希望有帮助

function firstImg($post_id) {
  $post = get_post($post_id);
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches[1][0];

  $urlLength = strlen(site_url());
  $first_img = substr($first_img, $urlLength);

  if(empty($first_img)){
    return false;
  }

  return $first_img;
}
if(!$firstImg){ // if available, update the post thumbnail
  update_post_meta( $post_id, '_thumbnail_id', 'link to default image here' );
} else { // else -> set thumbnail to default thumbnail
  update_post_meta( $post_id, '_thumbnail_id', $firstImg );
}