如何在the_content之外使用WP嵌入脚本 [英] How to use WP oembed script outside the_content
问题描述
我有一个自定义帖子类型视频,并希望使用WP默认显示脚本在指定区域显示youtube,dailymotion之类的视频。
因此,我使用的是自定义字段视频网址,但问题是嵌入在 the_content 中无法使用自定义字段。所以我该怎么做。或任何其他解决方案
I've a custom post type "video" and wanted to show videos like youtube, dailymotion in a specified area using WP default oembed script. So i'm using a custom field"video url", but the problem is that oembed work in the_content not with custom field. so how can i do this. or any other solution
推荐答案
如果自定义字段仅包含视频URL,例如 http:// www .youtube.com / watch?v = dQw4w9WgXcQ
,则可以使用 wp_oembed_get :
If the custom field only contains the video URL like http://www.youtube.com/watch?v=dQw4w9WgXcQ
, then you can get the oEmbed HTML code with wp_oembed_get:
$videourl = 'http://www.youtube.com/watch?v=dQw4w9WgXcQ';
$htmlcode = wp_oembed_get($videourl);
echo $htmlcode;
如果您的自定义字段不仅仅包含URL,则可以使用 the_content
过滤器执行与 the_content
函数相同的操作:
If your custom field contains more than just the URL, you could use the the_content
filter to do the same thing the the_content
-function does:
$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;
这篇关于如何在the_content之外使用WP嵌入脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!