Wordpress设置了来自post的第一张图片的特色图片

Wordpress设置了来自post的第一张图片的特色图片,wordpress,action,thumbnails,Wordpress,Action,Thumbnails,我尝试过类似答案的所有解决方案,但在WordPress5.0上似乎没有任何效果+ 当保存一篇文章时,我想将其特色图像设置为文章内容中的第一幅图像 function auto_set_featured( $post_id, $post, $update ) { $images = get_posts( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mi

我尝试过类似答案的所有解决方案,但在WordPress5.0上似乎没有任何效果+

当保存一篇文章时,我想将其特色图像设置为文章内容中的第一幅图像

function auto_set_featured( $post_id, $post, $update ) {
  $images = get_posts( array(
    'post_parent'    => $post_id,
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => 1
  ) );

  set_post_thumbnail( $post_id, $images[0]->ID );
}

add_action( 'save_post', 'auto_set_featured', 10, 3);
set_post_thumbnail()
中,如果我手动设置图像id,它会工作,但它似乎没有拾取
$images[0]->id
我不确定为什么不工作


注意:我正在测试内容中包含多个图像的帖子,因此
$images
应该返回一个数组。我还尝试在查询中使用
$post->ID
获取ID()
,但它不起作用。我还尝试为
post\u parent

手动添加帖子ID。这很简单。在wordpress 5.0+中,有一个gutenberg编辑器,所以只需从编辑器添加与特色图片相同的图片。它工作正常。我最近就是这样工作的。

这个答案与此无关,请删除您如何在内容中需要该图像作为内容的一部分?或者它只是在开始内容之前显示?你能把“第一张图片”说得更清楚些吗