WordPress Powerpress Youtube loop to show all posts with thumbnail

Add a loop to index.php (Main Index Template) to show all the blog’s posts/youtube videos with thumbnails below the homepage most recent single post.

/* added loop to return all wordpress posts */
	$rPosts = new WP_Query();
    	while ($rPosts->have_posts()) : $rPosts->the_post();

/* retrieve custom 'enclosure' value, which contains the Youtube embed code, from each post */	
		$enclosure = get_post_custom_values('enclosure');

/* don't render it immediately so that we can see what we're getting back */
		$enclosure = str_replace("<iframe", "", $enclosure[0]);

/* turn html content into an array so that we can target the Youtube video id */
		$enclosure = explode("http://www.youtube.com/embed/",$enclosure);
		$enclosure = explode("?", $enclosure[1]);

/* append the video id onto the youtube thumbnail url */
		$final = "<img src='http://img.youtube.com/vi/$enclosure[0]/default.jpg' />";

/* display thumb wrapped in an anchor */
		<a href="<?php the_permalink(); ?>"><?php echo $final; ?></a>

Leave a Reply

You must be logged in to post a comment.