• Hi guys,

    I’d like to have a loop in the sidebar which searches my posts for youtube video’s and returns a thumbnail if the post does have a video embedded.

    So I got this:

    <?php
    	global $wpdb;
    
    	$sql="SELECT 'ID','post_title','post_content'
    	FROM '{$wpdb->posts}' WHERE 'post_status'='publish'
    	AND 'post_content' like '%https://www.youtube.com/v/%'";
    
    	$posts=$wpdb->get_results($sql);
    
    	foreach($posts as $p) {
    		if (preg_match("/\/watch\?v=(.*?)[&\"]/i",$p->post_content,$match) ) {
    
    			$img = "https://i2.ytimg.com/vi/{$match[1]}/default.jpg";
    			echo $img;
    
    			flush();
    		}
    	}
    ?>

    But it does not work! Can someone help me out?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter 13Q13

    (@13q13)

    Got it.

    <?php
      query_posts('posts_per_page=4'); //define amount of posts
      while (have_posts()) : the_post();
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/\/watch\?v=(.*?)[&\"]/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
      if(!empty($first_img)){
        $img = "https://i2.ytimg.com/vi/{$first_img}/default.jpg";
        echo $img;
        //gives URL to thumbnail
      }
    
     endwhile;
     rewind_posts();
     ?>

    any way you can make it work for vimeo also? and not display a default thumb?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Return VIDEO thumbnail from posts’ is closed to new replies.