<?php get_header(); ?>

<main class="blog-page">
    <div class="blog-page__container">

        <h1 class="blog-page__title">Blog</h1>

        <?php
        $paged = get_query_var('paged') ?: 1;
        $q = new WP_Query([
            'post_type' => 'post',
            'posts_per_page' => 8,
            'paged' => $paged
        ]);
        ?>

        <?php if ($q->have_posts()) : ?>
            <div class="blog-grid">
                <?php while ($q->have_posts()) : $q->the_post(); ?>
                    <article class="post-card">
                        <a href="<?php the_permalink(); ?>" class="post-card__thumb">
                            <?php if (has_post_thumbnail()) : ?>
                                <?php the_post_thumbnail('medium'); ?>
                            <?php else : ?>
                                <img src="https://placehold.co/600x400" alt="">
                            <?php endif; ?>
                        </a>

                        <h2 class="post-card__title"><?php the_title(); ?></h2>

                        <a href="<?php the_permalink(); ?>" class="post-card__button">
                            Czytaj więcej
                        </a>
                    </article>
                <?php endwhile; ?>
            </div>

            <div class="blog-pagination">
                <?php
                echo paginate_links([
                    'total' => $q->max_num_pages,
                    'mid_size' => 2,
                    'prev_text' => '←',
                    'next_text' => '→'
                ]);
                ?>
            </div>

            <?php wp_reset_postdata(); ?>

        <?php else : ?>
            <p>Brak wpisów.</p>
        <?php endif; ?>

    </div>
</main>

<?php get_footer(); ?>
