如何在the_content之外使用WP嵌入脚本 [英] How to use WP oembed script outside the_content

查看:65
本文介绍了如何在the_content之外使用WP嵌入脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义帖子类型视频,并希望使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆