문제

I'm trying to get the first embed url from the post. I know wordpress auto-embeds them but can't find a method to just get the embeds.

                 $urls = array();
                 $p = get_post();
                 preg_match('@https?://(www.)?(youtube|vimeo)\.com/(watch\?v=)?([a-zA-Z0-9]+)@im', $p->post_content, $urls);
                 echo wp_oembed_get( $urls[0] );
도움이 되었습니까?

해결책

Since Wordpress 2.9, wp_oembed_get function has been included.

Example:

<?php $embed_code = wp_oembed_get($url); ?>

Here is the link to the function, to get the embeds:

http://codex.wordpress.org/Function_Reference/wp_oembed_get

다른 팁

This function will return the first [embed] shortcode:

function sofa_get_embed_shortcode($content) {
    preg_match('/\[embed(.*)](.*)\[\/embed]/', $content, $matches);
    return $matches;
}

Pass $post->post_content as the $content variable, and you'll receive an array of the [embed] shortcodes found.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top