<?php 
/**
* Template to display Related Posts
*  at bottom of single post.
*
* @author Pix & Hue
*/

$rilee_single_post_layout_option = get_theme_mod('rilee_single_post_layout');
$rilee_single_post_layout = isset($rilee_single_post_layout_option) && $rilee_single_post_layout_option != '' ? $rilee_single_post_layout_option : 'rilee-default-width';

$orig_post = $post;
global $post;  
 
$rilee_categories = get_the_category($post->ID);
$rilee_post_type = get_page_template_slug($post->ID); 

if($rilee_categories) {

	$category_ids = array();

	foreach($rilee_categories as $individual_category) $category_ids[] = $individual_category->term_id;

	if($rilee_single_post_layout == 'rilee-default-width') {

		$args = array(
			'category__in'     => $category_ids,
			'post__not_in'     => array($post->ID),
			'posts_per_page'   => 3, // Number of related posts that will be shown.
			'ignore_sticky_posts' => 1,
			'orderby' => 'rand'
		);

	} else {

		$args = array(
			'category__in'     => $category_ids,
			'post__not_in'     => array($post->ID),
			'posts_per_page'   => 4, // Number of related posts that will be shown.
			'ignore_sticky_posts' => 1,
			'orderby' => 'rand'
		);
	}	

	$my_query = new wp_query( $args );
	if( $my_query->have_posts() ) { ?>

		<div class="rilee-related-posts">
			<h3><?php esc_html_e('You May Also Like', 'rilee'); ?></h3>
			<div class="rilee-item-related-box">
				<?php while( $my_query->have_posts() ) {
					$my_query->the_post();

					//Related Item ?>
					<div class="rilee-item-related">

						<?php if(has_post_thumbnail()) { ?>
							<a href="<?php echo esc_url(get_permalink()); ?>">
								<?php $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium_large' ); ?>
								<div class="rilee-related-post-img" style="background-image:url(<?php echo esc_url($image_src[0]); ?>)"></div>
							</a>	
							<?php 
						} ?>	

						<div class="rilee-item-related-caption">
							<h4><a href="<?php echo esc_url(get_permalink()); ?>"><?php the_title(); ?></a></h4>
							<div class="rilee-related-post-date"><span class="post-date updated"><?php the_time( get_option('date_format') ); ?></span></div>
						</div>	

					</div>	
					<?php 
				} ?>		

			</div>	
		</div>	
		<?php
	}	
} 

$post = $orig_post;
wp_reset_postdata(); ?>