Php 从wordpress帖子检索嵌入的YouTube视频
tl;dr:我想从特定类别的帖子中提取嵌入的Youtube视频链接 我目前正在开发一个博客/文章网站。考虑下面的内容:我有一个索引页面,它有2个特色视频片段。假设我有一个查询和一个循环,从一个类别中检索2篇最新的文章。此类帖子总是以使用bootstrap嵌入的特色视频开头:Php 从wordpress帖子检索嵌入的YouTube视频,php,wordpress,Php,Wordpress,tl;dr:我想从特定类别的帖子中提取嵌入的Youtube视频链接 我目前正在开发一个博客/文章网站。考虑下面的内容:我有一个索引页面,它有2个特色视频片段。假设我有一个查询和一个循环,从一个类别中检索2篇最新的文章。此类帖子总是以使用bootstrap嵌入的特色视频开头: <div class="embed-responsive embed-responsive-16by9"> <iframe class="embed-responsive-item" src="..."
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="..."></iframe>
</div>
<!-- some text content follows -->
该函数从文章中提取内容并提取前30个单词,同时将所有html标记和图像分条。
我怎么能做相反的事呢?以某种方式检索嵌入的YouTube视频并删除其余视频?我有两个想法:
<span class="vid">...</span>
。。。
谢谢你的建议。我可以给你完成的代码,但是试着自己做,你将来可能会需要它 您应该使用DOM,并且可以使用正则表达式提取视频ID。 例如:
https://www.youtube.com/watch?v=EuQLMXyGQOE
ID为EuQLMXyGQOE
编辑:如果你被卡住了,这里是完成的代码
利用第一个答案中的资源以及更多的研究,我完成了以下功能:
<?php
$args = array(
'category_name' => 'featured-video',
'posts_per_page' => '2'
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
$content = $post->post_content;
$doc = new DOMDocument();
@$doc->loadHTML($content);
$iframes = $doc->getElementsByTagName('iframe');
foreach ($iframes as $frame) {
echo '<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item" src="'.$frame->getAttribute('src').'" frameborder="0" allowfullscreen></iframe>
</div><br>';
}
endwhile; endif;
wp_reset_query();
?>
自从3.6.0版WordPress引入了一个
获取媒体嵌入内容()函数来轻松获取嵌入内容
$media = get_media_embedded_in_content(
apply_filters( 'the_content', get_the_content() ), 'video'
);
print_r($media);
// results
[
0 => '<iframe>content</iframe>,
]
$media=get\u media\u embedded\u在内容中(
应用\u筛选器(“内容”,获取内容()),“视频”
);
印刷(媒体);
//结果
[
0=>'内容,
]
支持的音频类型为:“音频”、“视频”、“对象”、“嵌入”或“iframe”
您可以在此处找到更多信息。谢谢,我会尽力解决:)
$media = get_media_embedded_in_content(
apply_filters( 'the_content', get_the_content() ), 'video'
);
print_r($media);
// results
[
0 => '<iframe>content</iframe>,
]