很多实用WordPress程序搭建视频站的站长都在吐槽用WordPress做视频方面体验就不是很友好的,为了改进后大大提升了站点的用户体验,需要实现自动提取文章内视频作为封面,今天就分享一下WordPress自动获取文章内视频作为封面的方法。
将下方代码添加进function即可实现:
//提取视频封面
function catch_video_image() {
global $post, $posts;
$video_image = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;
ob_start();
ob_end_clean();
$output = preg_match_all(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/<video.+poster=[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”]([^\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”]+)[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”].*>/i\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, $post->post_content, $matches);
$video_image = $matches [1] [0];
return $video_image;
}
//正则提取字符串中视频url地址
function catch_that_video() {
global $post, $posts;
$first_video = \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’;
ob_start();
ob_end_clean();
$output = preg_match_all(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’/<video[^<>]*src=[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”]([^\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”]+)[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”][^<>]*>/im\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, $post->post_content, $matches);
$first_video = $matches [1] [0];
return $first_video;
}