<?php
$categories   = get_the_category( $post->ID ) ;
$category_ids = array();
$sidebar = is_active_sidebar('sidebar') ? true : false;

if ( $sidebar ) {
    $args['sizes'] = '(min-width: 1200px) 520px, (min-width: 992px) 430px, (min-width: 768px) 310px, (min-width: 576px) 200px, calc( 100vw - 70px )';
} else {
    $args['sizes'] = '(max-width: 575.98px) calc( 100vw - 24px ), (max-width: 767.98px) 516px, (max-width: 991.98px) 336px, (max-width: 1199.98px) 456px, (max-width: 1399.98px) 546px, 636px';
}

if ( $categories ) {    
    foreach( $categories as $individual_category ) {
        $category_ids[] = $individual_category->term_id;
    }

    $query = array(
    	'category__in'        => $category_ids,
    	'post__not_in'        => array( $post->ID ),
    	'posts_per_page'      => 2,
    	'ignore_sticky_posts' => 1,
    	'orderby'             => 'rand'
    );
    
    $loop = new WP_Query( $query );
    if ( $loop->have_posts() ) { ?>
    <div class="single-related-posts loza-box">
        <h3 class="post-related-title"><?php _e( 'You May Also Like', 'loza' ); ?></h3>
        <div class="loza-related-items">
            <?php while( $loop->have_posts() ) { ?>
                <?php $loop->the_post(); ?>
                <article <?php post_class(); ?>>
                    <?php if ( has_post_thumbnail() ) { ?>
    				<div class="post-thumbnails">
						<a href="<?php the_permalink(); ?>">
							<?php the_post_thumbnail( 'large', $args ); ?>
						</a>
    				</div>
    				<?php } ?>
                    <div class="post-details">
                        <div class="post-cats"><?php the_category(', '); ?></div>
                        <?php
                            the_title( '<h4 class="entry-title"><a href="'. esc_url( get_the_permalink() ) .'">', '</a></h4>' );
                            get_template_part( 'template-parts/post', 'meta' );
                        ?>
                    </div>
                </article>
            <?php } ?>
        </div>
    </div>
    <?php
    }
}

wp_reset_postdata();
