Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have embedded videos in the post (from youtube and other websites). Is it possible to get only the embedded video from the post and display on the homepage? There is some text before the video in the post, I do not want to display the text or anything else from the post, but only want to get the video. How that can be done? Thanks.

share|improve this question
    
In the post? Please explain this. And welcome to WordPress Stack Exchange! –  toscho Dec 12 '12 at 8:05

1 Answer 1

up vote 0 down vote accepted

Couple of assumptions before getting to solution. First, you have all embedded codes as iframe (almost all video sites export as iframe now) not as shortcode or object tag. Secondly, you want the video box not the video url which will be offcourse different for every video website. Put following code in functions.php

function the_post_video($post_id=NULL) {
   global $post;
   $target_post = $post;
   if($post_id !== NULL)
       $target_post = get_post($post_id);

   $matches = null;
   if(preg_match('/<iframe(.*?)\\/?>(<\\/iframe>)?/s', $post->content, $matches)) {
      return $matches[0];
   }
   return ''; // return empty if no iframe found.
}

Usage You would used the_post_video() instead of the_content();

To display posts on home page, you need to put follow regular method to display posts. There are number of ways one them is to use WP_Query in your front-page.php (you need to set WPAdmin->Settings->Reading->Frontpage to "Static page" -> any of your wp page)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.