Php WordPress-使第一个图像成为我的功能图像

Php WordPress-使第一个图像成为我的功能图像,php,wordpress,Php,Wordpress,我使用PHP在WP中使用WP_insert_post()创建新帖子 我想问问有没有办法让第一张图片成为这篇文章的特色图片 仅供参考:第一个映像不存储在服务器上,而是存储在外部 我真的需要你们的帮助。这是答案。你必须做一些事情,比如制作一个插件或者制作/修改一个主题来使用它 似乎希望始终通过媒体库添加特色图像。我相信用外部图像也可以做同样的事情,但是你必须让你的插件绕过WordPress系统 要在帖子中获得第一张图片,您可以在插件中执行类似操作 function your_function_nam

我使用PHP在WP中使用WP_insert_post()创建新帖子

我想问问有没有办法让第一张图片成为这篇文章的特色图片

仅供参考:第一个映像不存储在服务器上,而是存储在外部

我真的需要你们的帮助。

这是答案。你必须做一些事情,比如制作一个插件或者制作/修改一个主题来使用它

似乎希望始终通过
媒体库
添加特色图像。我相信用外部图像也可以做同样的事情,但是你必须让你的插件绕过WordPress系统

要在帖子中获得第一张图片,您可以在插件中执行类似操作

function your_function_name($content) {

        /** $content is a string and is the post content **/
        /** Here you can use strstr or preg_match to find the first
            occurence of an image in the post content. For example,
            this will print the matches at the end of the blog post.
            Once you have the first image you can do whatever you want with it..  **/

    $pattern = '/src="(.*\.(jpg|jpeg))"/i';
    preg_match($pattern, $content, $matches);
    $content .= var_dump($matches);
    return $content;
}
add_filter('the_content', 'your_function_name');
这是一个过滤器,因此每次在博客上显示帖子时,它都会运行。如果你只想做一次某件事,你会用不同的方式去做

如果有人知道比这更好/更简单的方法,我也会非常感兴趣