<?php 
$category_id = get_theme_mod('megapress_slideshow');

$slider_posts = get_posts(array(
  'posts_per_page' => -1,
  'category__in'    =>  $category_id,
  'meta_query' => array(array('key' => '_thumbnail_id')) //query having thumbnail
));

$number_slider_posts = sizeof($slider_posts);
if($number_slider_posts > 0) { 

?>
<div id="megapress_slider" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
   <?php for ($i=0; $i < $number_slider_posts ; $i++) { 
      $active = $i==0?'active':'';
      echo '<li data-target="#megapress_slider" data-slide-to="'. $i .'" class="'. $active .'"></li>';
    } ?> 
    
    
  </ul>
  
  <!-- The slideshow -->
  <div class="carousel-inner">
    <?php foreach($slider_posts as $key=>$slider): ?>
    <div class="carousel-item <?php echo $key == 0?'active':''?>">
      
     
      <?php echo get_the_post_thumbnail( $slider->ID , 'megapress-slider'); ?>
      
      <div class="carousel-caption">
          <h3> <a href="<?php echo get_permalink($slider->ID) ?>"><?php echo $slider->post_title ?></a></h3>
      </div>
      
    
    </div>
  <?php endforeach; ?>
    
    
  </div>
  
  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#megapress_slider" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#megapress_slider" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>
<?php } ?>