WordPress.org

Support

Support » Plugins and Hacks » Hacks » Dynamically retrieve youtube embed url from post

Dynamically retrieve youtube embed url from post

  • I’d like to be able to take a youtube video from the most recent post in a category and display it on my homepage. Check out my site: http://beachief.com/. There’s a video on the homepage, but its hard coded in there. I can get the attachment ID with this code:

    <?php
    global $wpdb;
    $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    ?>

    and display a video with this code:

    <iframe width="305" height="250" src="http://www.youtube.com/embed/YFbLRZCExBk" frameborder="0" allowfullscreen></iframe>

    But how can I display the video the ID is pointing to?

    I’m looking for something like this:

    <iframe width="305" height="250" src="<?php echo wp_get_attachment_url($attachment_id); ?>" frameborder="0" allowfullscreen></iframe>

    but that doesn’t work, because its not giving me the embed url. How can I get the video’s embed url?

Viewing 4 replies - 1 through 4 (of 4 total)
  • security_man
    Member

    @security_man

    check out the wordtube plugin… i think it is what you are looking for

    Thanks, but thats for embedding in posts. I need something I can embed directly in my php file. I tried this:
    [media id="$attachment_id"]
    But it only saw text

    security_man
    Member

    @security_man

    ah, then try do_shortcode in your php file

    I tried:
    <?php echo do_shortcode('[media id="$attachment_id"]') ?>
    but all it gave me was “[MEDIA not found]”

    Even if I did get this to work though, how could I resize the video to match my column width?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Dynamically retrieve youtube embed url from post’ is closed to new replies.