<?php

/**
 * The template for displaying all single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package WordPress
 * @subpackage NeatNote
 * @since 1.0.0
 */

get_header();

?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<?php
	/* Start the Loop */
	while (have_posts()): the_post();
		get_template_part('template-parts/content/content', 'single');
	endwhile;
	?>

	<!-- Related Post Section -->
	<?php
	$current_categories = get_the_category();

	if (!empty($current_categories)) {
		$category_ids = array();
		foreach ($current_categories as $current_category) {
			$category_ids[] = $current_category->term_id;
		}
		$args = array(
			'category__in' => $category_ids,
			'post__not_in' => array(get_the_ID()),
			'post_type' => 'post',
			'post_status' => 'publish',
			'posts_per_page' => 3,
			'orderby' => 'date',
			'order' => 'DESC'
		);
		$related_posts_query = new WP_Query($args);
	}

	// Display the related posts
	if ($related_posts_query->have_posts()):
	?>

		<section id="mainContent" class="NeatNote-RelatedBlogSection NeatNote-p70-0">
			<div class="NeatNote-wrapper">
				<div class="NeatNote-Heading NeatNote-text-center">
					<h2><?php esc_html_e('Related Posts', 'neatnote'); ?></h2>
				</div>
				<div class="NeatNote-relatedBlogSectionInner neatnote-pt50">
					<div class="NeatNote-relatedBlogRow">
						<?php while ($related_posts_query->have_posts()): $related_posts_query->the_post(); ?>
							<!-- Blog card -->
							<?php get_template_part('template-parts/content/related-blog', 'box'); ?>
						<?php endwhile; ?>
						<?php wp_reset_postdata(); ?>
					</div>
				</div>
			</div>
		</section>
	<?php endif; ?>

</article>
<?php
get_footer();
?>