admin管理员组文章数量:1422330
WordPress automatically embeds a youtube video's if I use:
[embed] [/embed ]
This is great, but it doesn't work if I use it in a template file. I have a custom field where the admin can put a URL to a YouTube video. I want to get the video in the single-post using the following code:
<?php
$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
?>
<div class="video">
[embed]<?php $url; ?>[/embed]
</div>
How can I convert the Youtube URL into an embed URL using the standard WordPress [embed] function?
WordPress automatically embeds a youtube video's if I use:
[embed] http://www.youtube/watch?v=Xog1T5dUxcw [/embed ]
This is great, but it doesn't work if I use it in a template file. I have a custom field where the admin can put a URL to a YouTube video. I want to get the video in the single-post using the following code:
<?php
$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
?>
<div class="video">
[embed]<?php $url; ?>[/embed]
</div>
How can I convert the Youtube URL into an embed URL using the standard WordPress [embed] function?
Share Improve this question edited May 5, 2018 at 6:02 CommunityBot 1 asked Aug 19, 2011 at 15:55 Sjoerd BoerrigterSjoerd Boerrigter 1692 silver badges10 bronze badges2 Answers
Reset to default 17Use wp_oembed_get( $url )
instead. Make sure you echo
it in your template file. So, something like this:
<?php
// tot necessary to set this but good if $url is coming from a function
$url = 'https://www.youtube/watch?v=jofNR_WkoCE';
// echo the function in your template to render the video
echo wp_oembed_get( $url );
?>
Normally you have to use do_shortcode in a template to place a shortcode outside of the content, however, I've had trouble with the embed shortcode specifically and could not make it work that way. I found this solution which works, but maybe there's a way to do this with do_shortcode and I've missed something.
<?php
$custom = get_post_custom($post->ID);
$url = $custom['_videoLink'][0];
if($url):
$shortcode = '[embed]'.$url.'[/embed]';
global $wp_embed;
echo $wp_embed->run_shortcode($shortcode);
endif;
?>
本文标签: Use embed filter in template files
版权声明:本文标题:Use [embed] filter in template files 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745363743a2655414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论