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.
1 Answer
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)