如何在the_content之外使用WP oembed脚本

Ste*_*inn 6 wordpress video custom-post-type

我有一个自定义的帖子类型"视频",并希望使用WP默认oembed脚本在指定区域中显示像youtube,dailymotion等视频.所以我正在使用自定义字段"视频网址",但问题是,在the_content中的oembed工作不是自定义字段.那我怎么能这样做呢.或任何其他解决方案

vst*_*stm 14

如果自定义字段只包含视频的网址一样http://www.youtube.com/watch?v=dQw4w9WgXcQ,那么你就可以得到与透过oEmbed HTML代码wp_oembed_get:

$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
Run Code Online (Sandbox Code Playgroud)

如果您的自定义字段不仅仅包含URL,您可以使用the_content过滤器执行the_content-function 所做的相同操作:

$content = "<h2>this video is great</h2>\n<p>check it out</p>\n"
  . "[embed]http://www.youtube.com/watch?v=dQw4w9WgXcQ[/embed]";

$htmlcode = apply_filters('the_content', $content);
echo $htmlcode;
Run Code Online (Sandbox Code Playgroud)