<?php
/*
Template Name: All Posts
*/

get_header(); // Hiển thị header

// Tạo đối tượng WP_Query để lấy tất cả bài viết
$args = array(
    'post_type' => 'post',
    'posts_per_page' => 24,
);

$all_posts_query = new WP_Query($args);
?>
    <main id="primary" class="site-main">
        <div class="container-fluid">
            <header class="page-header">
                <?php
                $args = array( 'home' => __('The Seasons Authentic' , 'storeethan') );
                woocommerce_breadcrumb($args);
                the_title( '<h1 class="page-title">', '</h1>' );
                ?>
            </header><!-- .page-header -->
            <div class="post_wrap">
                <?php if ( $all_posts_query->have_posts() ) : ?>
                    <div class="row">
                        <?php
                        while ( $all_posts_query->have_posts() ) :
                            $all_posts_query->the_post();
                            global $post;
                            $post_id = get_the_ID();
                            $thumbnail_id = get_post_thumbnail_id($post_id);
                            $image_url = wp_get_attachment_image_src($thumbnail_id, 'full');
                            $url = '';
                            if ($image_url) {
                                $url = $image_url[0];
                            }
                            $categories = get_the_category($post_id);
                            $cate = '';
                            foreach ($categories as $category) {
                                $cate .= $cate == '' ? $category->name : ', ' . $category->name;
                            }
                            $datetime = new DateTime($post->post_date);
                            $day_with_suffix = addOrdinalSuffix($datetime->format('j'));
                            $d =  $datetime->format('M') . ' ' .$day_with_suffix . ', ' . $datetime->format('Y');
                            ?>
                            <div class="col-md-3 col-12 post">
                                <a href="<?php echo get_permalink(); ?>">
                                    <div class="card_post">
                                        <img src="<?php echo $url; ?>" alt="Ảnh bài viết">
                                        <div class="props-of-post">
                                            <div class="type_post">
                                                <?php echo $cate; ?>
                                            </div>
                                            <div class="name_post">
                                                <?php the_title(); ?>
                                            </div>
                                            <div class="date_post">
                                                <!--July 14th, 2024-->
                                                <?php echo $d; ?>
                                            </div>
                                        </div>
                                    </div>
                                </a>
                            </div>
                        <?php endwhile; ?>
                    </div>
                    <?php if (get_next_posts_link(null, $all_posts_query->max_num_pages)) : ?>
                    <div class="d-flex justify-content-center align-items-center">
                        <a href="" id="more_post" class="btn btn_see_all">
                            <?php _e('Xem thêm' , 'storeethan') ?>
                        </a>
                    </div>
                    <?php endif; ?>
                <?php
                else :
                    get_template_part( 'template-parts/content', 'none' );

                endif;
                ?>
            </div>
        </div>
    </main>

<?php
get_footer();
