有谁能帮我吗,我需要扫描帖子找到youtube视频。如果帖子包含视频(www.youtube.com/embed/youtubepostid
),那么我希望将该url复制到带有"iFrame"键的自定义字段中。
我绕着圈寻找答案,每当我试着哭的时候,我就站起来跟水务署说:
有指向正确方向的指示吗?
发布于 2014-11-08 16:01:06
我编写了一些代码来解决我们常见的问题;)如果您想要显示格式化的嵌入视频(在post内容之外),我也添加了一些内容。
I -将这些函数添加到“functions.php”文件中
1-从内容中删除youtube嵌入
function remove_video( $content ) {
$postOutput = preg_replace('/<iframe[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_video', 100 );
2 -从内容捕获并返回youtube嵌入
function catch_first_youtube_video() {
global $post, $posts;
// Catch youtube iframe src
preg_match_all('/(http|https).*(yout).*/i', $post->post_content, $matches);
$youtubeUrl = $matches[0][0];
// Set the new meta for your post.
add_post_meta($post->ID, 'iFrame', $youtubeUrl)
// Remove http: or https:
$formated = preg_replace('/(http:|https:|watch\\?v=)/i','', $youtubeUrl);
// Formated string '//youtube.com/embed/XXXXXXX'
$formated =str_replace('youtube.com/','youtube.com/embed/', $formated );
return '<iframe src="'.$formated .'"></iframe>';
}
II -在post模板内容中调用这些函数
1 -在没有嵌入视频的情况下,在post模板中显示帖子的内容
remove_filter(the_content(),'remove_video', 100 );
2 -显示嵌入视频,如果你想要什么!
<?= catch_first_youtube_video(); ?>
还有哇哦!享受吧!(对不起,我的英语很差)
发布于 2014-12-22 19:47:02
我试着实现,但得到一个警告和一个通知。
add_post_meta($post->ID, 'iFrame', $youtubeUrl);
需要一个;来关闭。这样我就可以修正警告了。$youtubeUrl = $matches[0][0];
中,我不知道如何修复它:(谢谢你的帮助。
https://stackoverflow.com/questions/25205342
复制相似问题