0

I crating custom wordpress themes for video related website, client can embed youtube video, in index.php only show thumbnail image and title of video then when click thumbnail it goes to single.php & show the video,

Plase any one help me, I try Video Thumbnails it not work me.

1

1 Answer 1

1

To solve this:

1. Add custom meta to each post which take youtube url( name of custom meta in code youtube).

2. In index.php loop:

  while(have_posts()){

    the_post();

   //use [get_post_meta][3] to get youtube post meta
   $youtube_img = 'http://img.youtube.com/vi/'.substr( get_post_meta($post-ID,'youtube') ).'/0.jpg';

   //$youtube_img is your youtube video image url
   echo '<a href="'.get_permalink().'">';
   echo '<img src="'.$youtube_img.'" title="'.get_the_title().'" alt="youtube video image for'.get_the_title().'"/>';
   echo'</a>';
   }

3. In single.php loop (set iframe width as you design need)

<?php
while(have_posts()){
    the_post();

    //use [get_post_meta][3] to get youtube post meta
    $youtube_link = get_post_meta($post-ID,'youtube');   
    ?>    

    //$youtube_link is your youtube url
    <iframe width="328" height="233" src="<?php echo str_replace('watch?v=','embed/', $youtube_link); ?>" frameborder="0" allowfullscreen></iframe>'
    <?php
}
2
  • This my code all so not working <?php if (have_posts()) : while(have_posts()){ the_post(); //use [get_post_meta][3] to get youtube post meta $youtube_img = 'img.youtube.com/vi/'.substr( get_post_meta($post-ID,'youtube') ).'/0.jpg'; //$youtube_img is your youtube video image url echo '<a href="'get_permalink().'">'; echo '<img src="'.$youtube_img.'" title="'.get_the_title().'" alt="youtube video image for'.get_the_title().'"/>'; echo'</a>;' } <?php endwhile; endif; ?>
    – Faiq Adam
    Jul 8, 2013 at 18:09
  • their are some error in php code formatting in your code. Try this <?php if (have_posts()) : while(have_posts()){ the_post(); //use [get_post_meta][3] to get youtube post meta $youtube_img = 'http://img.youtube.com/vi/'.substr( get_post_meta($post-ID,'youtube') ).'/0.jpg'; //$youtube_img is your youtube video image url echo '<a href="'.get_permalink().'">'; echo '<img src="'.$youtube_img.'" title="'.get_the_title().'" alt="youtube video image for'.get_the_title().'"/>'; echo'</a>'; } endif; ?> Jul 10, 2013 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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