Dynamically retrieve youtube embed url from post (5 posts)

  1. Cbas23
    Posted 5 years ago #

    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:

    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?

  2. security_man
    Posted 5 years ago #

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

  3. Cbas23
    Posted 5 years ago #

    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

  4. security_man
    Posted 5 years ago #

    ah, then try do_shortcode in your php file

  5. Cbas23
    Posted 5 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic