<div class="row">

<?php

	if ( $posts_query->have_posts() ) : while ( $posts_query->have_posts() ) : $posts_query->the_post(); 
		$child_posts = types_child_posts('cta-ad-child');
	 $child_posts_sorted = usort($child_posts, function ($a, $b){
		$a_viewOrder = intval($a->fields['cta-view-order']);
		$b_viewOrder = intval($b->fields['cta-view-order']);
		if ($a_viewOrder == $b_viewOrder) { return 0; }
		return ($a_viewOrder < $b_viewOrder) ? -1 : 1;
	});

	
	
	foreach ($child_posts as $post) :
		$cta_ad_title 			= types_render_field("cta-ad-title");
    	$cta_ad_link_text 		= types_render_field("cta-link-text");
        $cta_ad_image 			= types_render_field("cta-ad-image", array("output"=>"raw"));
        $cta_ad_image_text 		= types_render_field("cta-ad-image-text", array("output"=>"raw"));
        $cta_ad_text 			= types_render_field("cta-ad-text", array("output"=>"html"));
        $cta_ad_link 			= types_render_field("cta-ad-url", array("output"=>"raw"));
        $cta_ad_new_window 		= types_render_field("cta-ad-new-window");
        $cta_ad_random_time 	= date("Y-m-dH");
?>
	<div class="col-sm-4 cta-box-content<?php if(!empty($cta_ad_image_text)){ ?> cta-box-has-text-img<?php } ?>">
		<a href="<?php echo $cta_ad_link; ?>" <?php echo $cta_ad_new_window == "1" ? "target=\"_blank\"" : ""?> class="cta-box-item" style="background: url(<?php echo $cta_ad_image; ?>);">
			<?php if(!empty($cta_ad_image_text)){ ?>
			<img src="<?php echo $cta_ad_image_text; ?>" alt="" />
			<?php } ?>

			<?php if(!empty($cta_ad_title)){ ?>
			<h1><?php echo $cta_ad_title; ?></h1>
			<?php } ?>
			<span class="cta-link-text"><?php echo $cta_ad_link_text; ?> <span class="fp-icon-chevron-right"></span></span>
		</a>
	</div>
<?php
	endforeach;
	endwhile; endif;
?>

</div>
