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

if ( $sidebar ) {
    $args['sizes'] = '(min-width: 1200px) 355px, (min-width: 992px) 265px, (min-width: 768px) 320px, (min-width: 576px) 470px, calc( 100vw - 70px )';
} else {
    $args['sizes'] = '(min-width: 1200px) 520px, (min-width: 992px) 430px, (min-width: 768px) 650px, (min-width: 576px) 470px, calc( 100vw - 70px )';
}

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 blog-grid kayla-box">
        <h3 class="post-related-title"><?php _e( 'You May Also Like', 'kayla' ); ?></h3>
        <div class="kayla-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();
