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

get_header();
?>
<?php
    $post_id = get_the_ID();
    $crt_builder_with_elementor_thumbnail = get_theme_mod('crt_manage_single_thumbnail', 'outer-thumb');
    $crt_builder_with_elementor_args = crt_builder_with_elementor_archive_layout();
    $crt_builder_with_elementor_col_one = $crt_builder_with_elementor_args['col_one'];
    $crt_builder_with_elementor_col_two = $crt_builder_with_elementor_args['col_two'];
    $crt_builder_with_elementor_sidebar_position = $crt_builder_with_elementor_args['sidebar'];
    $crt_builder_with_elementor_related_layout = $crt_builder_with_elementor_args['layout'];
    $crt_builder_with_elementor_layout = get_theme_mod('crt_manage_single_sidebar', 'right-sidebar');
    if(get_post_format() == 'aside') {
        $crt_builder_with_elementor_layout = 'right-sidebar';
    }
?>
<main id="content" class="site-main single-<?php echo esc_attr($crt_builder_with_elementor_layout); ?>" itemscope="" itemtype="https://schema.org/CreativeWork">
    <?php do_action('crt_builder_with_elementor_single_header'); ?>

    <section class="block-content mt-4">
        <div class="container">
            <div class="">
                <div class="row">
                    <div class="<?php echo esc_attr($crt_builder_with_elementor_col_one); ?>">
                        <div class="single-detail__inner">
                        <?php
                        while ( have_posts() ) :
                            the_post();

                            get_template_part( 'template-parts/content', 'single' );

                            do_action('crt_builder_with_elementor_post_navigation');

                            do_action( 'crt_builder_with_elementor_author', $post );

                            // 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;

                            // Related Posts
                            if ( is_singular( 'post' ) ) {
                                $crt_builder_with_elementor_related_heading = get_theme_mod( 'crt_manage_single_related_heading', __( 'Related Posts','crt-builder-with-elementor' ) );
                                $crt_builder_with_elementor_grid = str_contains($crt_builder_with_elementor_related_layout, 'masonry');
                                $crt_builder_with_elementor_cat_content_id      = get_the_category( $post->ID )[0]->term_id;
                                $crt_builder_with_elementor_crt_manage_single_related_args                = array(
                                    'cat'            => $crt_builder_with_elementor_cat_content_id,
                                    'post__not_in'   => array( $post->ID ),
                                    'orderby'        => 'rand',
                                );
                                $query               = new WP_Query( $crt_builder_with_elementor_crt_manage_single_related_args );
                                if ( $query->have_posts() ) :
                                    ?>
                                    <div class="related-posts mt-4">
                                        <h2><?php echo esc_html( $crt_builder_with_elementor_related_heading ); ?></h2>
                                        <div class="<?php echo esc_attr($crt_builder_with_elementor_grid ? 'grid '.$crt_builder_with_elementor_related_layout:'row') ?>">
                                            <div class="grid__col-sizer"></div>
                                            <?php
                                                while ( $query->have_posts() ) :
                                                $query->the_post();
                                                get_template_part( 'template-parts/content', $crt_builder_with_elementor_related_layout );
                                                endwhile;
                                            ?>
                                        </div>
                                    </div>
                                    <?php
                                endif;
                                wp_reset_postdata();
                            }



                        endwhile; // End of the loop.
                        ?>
                        </div>
                    </div>
                    <div class="<?php echo esc_attr($crt_builder_with_elementor_col_two); ?>">
                        <?php if ($crt_builder_with_elementor_col_two != 'd-none'): ?>
                            <aside id="secondary" class="widget-area">
                                <?php
                                if ( is_active_sidebar( $crt_builder_with_elementor_sidebar_position ) ) {
                                    dynamic_sidebar( $crt_builder_with_elementor_sidebar_position );
                                }
                                ?>
                            </aside>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
</main><!-- #main -->

<?php
get_footer();
