<?php 

function news_shortcode($atts) {
    $atts = shortcode_atts(
        array(
            // Define any attributes for the shortcode if needed
        ),
        $atts,
        'news'
    );

    ob_start();

    $news_args = array(
        'post_type'      => array('post', 'downloads'),
        'posts_per_page' => 4,
        'orderby' => 'date',
        'order' => 'DESC',
        'tax_query'      => array(
            'relation' => 'OR',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array('news', 'blog'),
            ),
            array(
                'taxonomy' => 'file_category',
                'field'    => 'slug',
                'terms'    => array('annual-reports', 'newsletters', 'press-statements', 'petitions', 'proceedings-reports', 'advocacy-letters', 'memoranda', 'policy-briefing-papers', 'communiques', 'case-files', 'research-reports', 'factsheets', 'weekly-energy-updates', 'case-files', 'other-publications'),
            ),
        ),
    );

    $news_query = new WP_Query($news_args);

    if ($news_query->have_posts()) :
        ?>
        <div class="row">
            <?php
            while ($news_query->have_posts()) :
                $news_query->the_post();
                $categories = get_the_category();
                $file_categories = get_the_terms(get_the_ID(), 'file_category');
                ?>
                <div class="col-xl-3 col-lg-4 col-sm-6">
                    <article>
                        <a href="<?= esc_url(get_permalink()); ?>" style="cursor: pointer">
                            <div class="card shadow-sm mb-3 border-0" data-aos="fade-up">
                                <div class="post-thumbnail">
                                    <?php
                                    // Display the post thumbnail
                                    if (has_post_thumbnail()) {
                                        the_post_thumbnail('medium'); // You can use different sizes
                                    } else {
                                        echo '<img src="' . get_template_directory_uri() . '/assets/images/placeholder-image.jpg" alt="Afiego" class="img-fluid"/>';
                                    }
                                    ?>
                                    <div class="catz">
                                        <?php
                                        // Display categories
                                        if ($categories) {
                                            foreach ($categories as $category) {
                                                echo esc_html($category->name);
                                            }
                                        }

                                        // Display file categories
                                        if ($file_categories && !is_wp_error($file_categories)) {
                                            foreach ($file_categories as $file_category) {
                                                echo esc_html($file_category->name);
                                            }
                                        }
                                        ?>
                                    </div><!-- Categories !-->
                                </div><!-- Thumbnail !-->
                                <div class="card-body">
                                    <h3><?php the_title(); ?></h3>
                                </div><!-- card body !-->
                            </div><!-- card !-->
                        </a>
                    </article>
                </div><!-- col !-->
            <?php endwhile;
            wp_reset_postdata();
            ?>
        </div><!-- Row !-->
    <?php else :
        echo 'No news found.';
        echo '</div>';
    endif;

    return ob_get_clean();
}

add_shortcode('newz', 'news_shortcode');
