<?php
/**
 * The template for displaying all single posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
 *
 * @package Mindful Blog
 */

get_header();
?>
<main id="primary" class="site-main">

	<?php
	while ( have_posts() ) :
		the_post();
		get_template_part( 'template-parts/content', 'single' );

		do_action( 'mindful_blog_post_navigation' );

		if ( is_singular( 'post' ) ) {
			$mindful_blog_related_posts_label = get_theme_mod( 'mindful_blog_post_related_post_label', __( 'Related Posts', 'mindful-blog' ) );
			$mindful_blog_args = array(
				'posts_per_page' => 3,
				'post__not_in'   => array( $post->ID ),
				'orderby'        => 'rand',
			);

			$mindful_blog_post_categories = get_the_category( $post->ID );
			if ( ! empty( $mindful_blog_post_categories ) ) {
				$mindful_blog_args['cat'] = $mindful_blog_post_categories[0]->term_id;
			}

			$mindful_blog_query = new WP_Query( $mindful_blog_args );

			if ( $mindful_blog_query->have_posts() ) :
				?>
				<div class="related-posts">
					<h2 class="asn-related-title"><?php echo esc_html( $mindful_blog_related_posts_label ); ?></h2>
					<div class="row">
						<?php
						while ( $mindful_blog_query->have_posts() ) :
							$mindful_blog_query->the_post();
							?>
							<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
								<div class="mag-post-single">
									<div class="mag-post-img">
										<?php the_post_thumbnail( 'post-thumbnail' ); ?>
									</div>
									<div class="mag-post-detail">
										<?php the_title( '<h5 class="entry-title mag-post-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h5>' ); ?>
										<div class="mag-post-excerpt">
											<?php the_excerpt(); ?>
										</div><!-- .entry-content -->
									</div>
								</div>
							</article>
							<?php
						endwhile;
						wp_reset_postdata();
						?>
					</div>
				</div>
				<?php
			endif;
		}

			// If comments are open or we have at least one comment, load up the comment template.
		if ( comments_open() || get_comments_number() ) :
			comments_template();
	endif;

		endwhile; // End of the loop.
	?>

	</main><!-- #main -->
	<?php
	if ( mindful_blog_is_sidebar_enabled() ) {
		get_sidebar();
	}
	get_footer();
