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

时间:2013-02-18 05:13:27

标签: wordpress video custom-post-type

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

2 个答案:

答案 0 :(得分:12)

如果自定义字段仅包含http://www.youtube.com/watch?v=dQw4w9WgXcQ等视频网址,则您可以使用wp_oembed_get获取oEmbed HTML代码:

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

如果您的自定义字段不仅包含网址,您可以使用the_content过滤器执行the_content - 函数执行的操作:

$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;

答案 1 :(得分:-1)

以下是您问题的完整答案。它也是一种更简洁,更快捷的方法,使用wp_oembed_get而不是短代码。当然,请将video_url更改为自定义字段的名称。

此代码检查video_url字段是否为空,然后oEmbeds视频。

<?php if (!((get('video_url', TRUE))=='')) {
    echo wp_oembed_get( get('video_url', true) );
}?>