<?php
/**
 * Single post template
 *
 * @package WordPress
 */

get_header();
?>


<?php while ( have_posts() ) : the_post(); ?>
    <div class="single-post-container">
        <div class="container">
            <div class="row">
                <div class="col">
                    <h1 class="mb-4"><?php the_title(); ?></h1>
                    <div class="single-post-img">
                        <?php if( has_post_thumbnail() ) : ?>
                            <?php
                                $image_id = get_post_thumbnail_id( $post->ID, 'medium' );
                                $alt_text = get_post_meta( $image_id , '_wp_attachment_image_alt', true );
                            ?>
                            <img class="img-fluid" src="<?php echo the_post_thumbnail_url( $image_id, 'medium' ) ?>" alt="<?php echo $alt_text ;?>" />
                         <?php endif; ?>
                    </div>
                    <div class="single-post-content">
                        <small><?php the_time('F jS, Y') ?></small>
                        <!-- Social Media Share buttons -->
                        <div id="sharethis" style="margin:27px 0px 27px 0px;"><?php echo sharethis_inline_buttons(); ?></div>
                        <?php the_content(); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php endwhile; ?>

<!-- Read More Posts section BEGIN -->
<section class="container my-4 related-posts-container">
    <div class="row mb-2">
        <div class="col-12">
            <h3 class="related-posts-title">
                Read more posts on 
                <?php 
                    $cat_names = get_the_category( $id );
                    foreach ( $cat_names as $cat_name ) { 
                        echo ' - ' . $cat_name->name; 
                    }; 
                ?>
            </h3>
        </div>
    </div>

    <div class="row">
        <?php
            $related = get_posts( 
                array( 
                    'category__in' => wp_get_post_categories( $post->ID ), 
                    'numberposts' => 4, 
                    'post__not_in' => array( $post->ID ) 
                ) 
            );

            if( $related ) foreach( $related as $post ) {
            setup_postdata($post); 
        ?>

        <div class="col-12 col-md-6 col-lg-3 mb-4 related-post">
            <div class="pb-2 post-img">
                <?php
                    $read_more_image_id = get_post_thumbnail_id( $post->ID, 'medium' );
                    $read_more_alt_text = get_post_meta( $read_more_image_id , '_wp_attachment_image_alt', true );
                ?>
                <?php if( !empty(get_the_post_thumbnail()) ) : ?>
                    <a href="<?php the_permalink(); ?>">
                        <img class="img-fluid related-img" src="<?php echo the_post_thumbnail_url( $read_more_image_id, 'medium' ); ?>" alt="<?php echo $read_more_alt_text; ?>" />
                    </a>    
                <?php else : ?>
                    <div class="d-flex justify-content-center align-items-center img-placeholder">
                        <img class="img-fluid" src="<?php echo $company_logo['url']; ?>" alt="<?php echo $company_logo['alt'] ?>" />
                    </div>
                <?php endif; ?>
            </div>
            <a href="<?php the_permalink(); ?>">
                <h4 class="related-title"><?php the_title(); ?></h4>
            </a>
        </div>
        <?php } ?>
    </div>
<?php  wp_reset_postdata(); ?>
</section>
<!-- Read More Posts section END -->


<?php get_footer(); ?>
